-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamictile.js
144 lines (131 loc) · 4.66 KB
/
dynamictile.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Game.DynamicTile = function(properties) {
properties = properties || {};
// Call the glyph's construtor with our set of properties
Game.DynamicGlyph.call(this, properties);
// Set up the properties
this._name = properties['name'];
this._walkable = properties['walkable'] || false;
this._diggable = properties['diggable'] || false;
this._blocksLight = (properties['blocksLight'] !== undefined) ? properties['blocksLight'] : true;
this._description = properties['description'] || '';
this._flammable = properties['flammable'] || false;
this._lifespan = properties['lifespan'] || 5;
this._speed = properties['speed'] || 1000;
this._defaultForeground = this._foreground;
this._defaultBackground = this._background;
this._isHazard = properties['isHazard'] || false;
};
// Make items inherit all the functionality from glyphs
Game.DynamicTile.extend(Game.DynamicGlyph);
// Standard getters
Game.DynamicTile.prototype.isWalkable = function() {
return this._walkable;
}
Game.DynamicTile.prototype.isDiggable = function() {
return this._diggable;
}
Game.DynamicTile.prototype.isBlockingLight = function() {
return this._blocksLight;
}
Game.DynamicTile.prototype.isFlammable = function() {
return this._flammable;
}
Game.DynamicTile.prototype.getDescription = function() {
return this._description;
};
Game.DynamicTile.prototype.getNeighborPositions = function() {
let tiles = [];
// Generate all possible offsets
for (let dX = -1; dX < 2; dX ++) {
for (let dY = -1; dY < 2; dY++) {
// Make sure it isn't the same tile
if (dX === 0 && dY === 0 || (dX === dY)) {
continue;
}
tiles.push({x: this._x + dX, y: this._y + dY});
}
}
return Game.randomize(tiles);
};
Game.DynamicTile.prototype.setX = function(x) {
this._x = x;
}
Game.DynamicTile.prototype.setY = function(y) {
this._y = y;
}
Game.DynamicTile.prototype.setMap = function(map) {
this._map = map;
}
Game.DynamicTile.prototype.getX = function() {
return this._x;
}
Game.DynamicTile.prototype.getY = function() {
return this._y;
}
Game.DynamicTile.prototype.getMap = function() {
return this._map;
}
Game.DynamicTile.prototype.getSpeed = function() {
return this._speed;
};
Game.DynamicTileMixins = {};
Game.DynamicTileMixins.Actor = {
name: 'Actor',
act: function() {
if (this._lifespan > 0) {
this._lifespan--;
} else {
this.getMap().removeDynamicTile(this);
}
// Just do this part once, we don't grow new flamable substances
if (this.hasMixin("Spreadable")){
if (!this.hasTriedSpreading()){
let newTile = '';
let neighbors = this.getNeighborPositions();
// see if they match this._spreadSubstance
for (let neighbor of neighbors){
let tile = this._map.getTile(neighbor.x, neighbor.y);
if (this._spreadSubstance && tile.isFlammable()){
if (this._name = 'fireTile'){
newTile = Game.DynamicTileRepository.create('fireTile')
} else if (this._name = 'hellFireTile'){
newTile = Game.DynamicTileRepository.create('hellFireTile')
}
this._map.addDynamicTile(newTile, neighbor.x, neighbor.y);
}
}
this.setTriedSpreading();
}
}
// if in field of view
let map = this.getMap();
// Compute the FOV and check if the coordinates are in there.
let canSee = false;
let otherX = map._player.getX();
let otherY = map._player.getY();
this.getMap().getFov().compute(
this.getX(), this.getY(), 100,
function(x, y, radius, visibility) {
if (x === otherX && y === otherY) {
canSee = true;
}
});
if (canSee){
this._foreground = ROT.Color.toHex(ROT.Color.randomize(ROT.Color.fromString(this._defaultForeground), [10, 10, 10]));
this._background = ROT.Color.toHex(ROT.Color.randomize(ROT.Color.fromString(this._defaultBackground), [10, 10, 10]));
}
}
};
Game.DynamicTileMixins.Spreadable = {
name: 'Spreadable',
init: function(template) {
this._spreadSubstance = template['spreadSubstance'] || 'flammable';
this._triedSpreading = false;
},
hasTriedSpreading: function(){
return this._triedSpreading;
},
setTriedSpreading: function(){
this._triedSpreading = true;
}
};