aboutsummaryrefslogtreecommitdiff
path: root/features/dungeonMap2/DungeonMapData.js
blob: 15ffccac40aa962e94d57c7eac803eb390275fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { m } from "../../../mappings/mappings"
import DungeonMapRoom from "./DungeonMapRoom"

const BufferedImage = Java.type("java.awt.image.BufferedImage")

class DungeonMapData {
    constructor(floor) {
        this.floor = floor

        this.greenRoom = undefined
        /**
         * @type {Map<String,DungeonMapRoom>}
         */
        this.rooms = new Map()

        this.image = undefined
    }

    setRoom(x, y, rotation, id) {
        let locstr = x + "," + y

        if (this.rooms.get(locstr) && this.rooms.get(locstr).roomId !== id) {
            this.rooms.get(locstr).setId(id)
            this.mapChanged()
            return
        }

        let room = DungeonMapRoom.fromId(id, x, y, rotation)

        if (room.type === DungeonMapRoom.TYPE_SPAWN) {
            this.greenRoom = locstr
        }
        console.log(room.type)

        this.rooms.set(locstr, room)

        this.mapChanged()
    }

    mapChanged() {
        if (this.image) {
            this.image.getTexture()[m.deleteGlTexture]()
            this.image = undefined
        }
    }

    /**
     * @returns {Image}
     */
    getImage() {
        if (!this.image) {
            this.image = new Image(this.render())
        }
        return this.image
    }

    /**
     * @returns {BufferedImage}
     */
    render() {
        //create 256x256 image
        let image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB)

        //if green room data not found yet then map should be empty
        if (!this.greenRoom) return image

        //create graphics rendering context
        let graphics = image.createGraphics()

        //render rooms
        for (let data of this.rooms.entries()) {
            let room = data[1]
            room.draw(graphics, 256, 256)
        }

        return image
    }
}

export default DungeonMapData