-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.js
226 lines (196 loc) · 6.31 KB
/
Scene.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
Adventurous.Scene = function (obj)
{
this.name = obj.name;
if(obj.scales != null)
{
this.scales = obj.scales;
}
if(this.name != Adventurous.Constants.OFFSTAGE_SCENE)
{
this.background = game.add.sprite(0,0,this.name);
game.physics.enable(this.background, Phaser.Physics.ARCADE, true);
this.map = game.add.tilemap(this.name);
var graphMap = [];
for(var i = 0 ; i < this.map.layers[0].data.length; i++)
{
var mapRow = this.map.layers[0].data[i];
var arr = [];
for(var j = 0; j < mapRow.length; j++)
{
arr[arr.length] = mapRow[j].index;
}
graphMap[i] = arr;
}
this.graph = new Graph(graphMap);
}
else
{
this.background = game.add.sprite(0,0);
}
this.things = new Array(); //a subset of Adventurous.Game.thingsArray
this.triggerAreas = new Array();
this.exits = new Array();
this.entrances = {};
this.init(obj);
GraphNodeType.WALL = 1; //zone tilemap is 1-indexed
GraphNodeType.OPEN = 2;
Adventurous.scenes[this.name] = this;
this.hide();
};
Adventurous.Scene.prototype =
{
update: function ()
{
for(var i = 0; i < this.triggerAreas.length; i++)
{
this.triggerAreas[i].update();
}
for(var i = 0; i < this.exits.length; i++)
{
this.exits[i].update();
}
for(var i = 0; i < this.things.length; i++)
{
this.things[i].update();
}
},
findPath: function(thing,x,y)
{
var endTileX = Math.floor(x / Adventurous.Constants.TILE_SIZE);
var endTileY = Math.floor(y / Adventurous.Constants.TILE_SIZE);
var start = this.graph.nodes[thing.getTileY()][thing.getTileX()];
var end = this.graph.nodes[endTileY][endTileX];
var result = astar.search(this.graph.nodes, start, end, true);
result.splice(0,0,start);
result = this.smoothPath(result);
var pathInPixels = new Array(result.length);
pathInPixels[0] = new Phaser.Point(thing.sprite.x,thing.sprite.y);
if(result.length == 1)
{
pathInPixels[1] = new Phaser.Point(x,y);
}
else
{
for(var i = 1; i < result.length; i++)
{
//Note: the X and Y on the graph seem to be mixed up (???) so it makes the code here a bit funny
if(i != result.length - 1)
{
pathInPixels[i] = new Phaser.Point((result[i].y + 0.5) * Adventurous.Constants.TILE_SIZE, (result[i].x+0.5) * Adventurous.Constants.TILE_SIZE);
}
else
{
pathInPixels[i] = new Phaser.Point(x,y);
}
}
}
return pathInPixels;
},
//Removes superfluous intermediary tiles
smoothPath: function(path)
{
var keepOnSmoothing = true;
while(keepOnSmoothing)
{
keepOnSmoothing = false;
for(var i = 1; i < path.length-1; i++)
{
if(this.lineOfSight(path[i-1].y,path[i-1].x,path[i+1].y,path[i+1].x))
{
path.splice(i,1);
i--;
keepOnSmoothing = true;
}
}
}
return path;
},
lineOfSight: function(x1,y1,x2,y2)
{
var point = new Phaser.Point(x1,y1);
var dir = new Phaser.Point(x2-x1,y2-y1).normalize();
dir.x = dir.x * .25;
dir.y = dir.y * .25;
var passed = false;
while(!passed)
{
point.x += dir.x;
point.y += dir.y;
if(Adventurous.Constants.WALKABLE_TILES.indexOf(this.map.getTile(Math.floor(point.x),Math.floor(point.y)).index) == -1)
{
return false;
}
passed = ( (x2==x1) || (x2 > x1 && point.x >= x2) || (x2 < x1 && point.x <= x2)) &&
((y2 == y1) || (y2 > y1 && point.y >= y2) || (y2 < y1 && point.y <= y2));
}
return true;
},
init: function(obj)
{
//things
if(obj.things != null)
{
for(var i = 0; i < obj.things.length; i++)
{
var thing = new Adventurous.Thing(obj.things[i]);
Adventurous.thingsMap[thing.name] = thing;
Adventurous.thingsArray[Adventurous.thingsArray.length] = thing;
if(thing.name != Adventurous.Constants.PLAYER_NAME)
{
this.things[this.things.length] = thing;
}
else
{
Adventurous.startingScene = this.name;
}
}
}
//triggerAreas
if(obj.triggerAreas != null)
{
for(var i = 0; i < obj.triggerAreas.length; i++)
{
this.triggerAreas[i] = new Adventurous.TriggerArea(obj.triggerAreas[i]);
}
}
if(obj.entrances != null)
{
for(var i = 0; i < obj.entrances.length; i++)
{
this.entrances[obj.entrances[i].name] = obj.entrances[i];
}
}
},
hide: function()
{
this.background.visible = false;
for(var i = 0; i < this.things.length; i++)
{
this.things[i].stopTalking();
}
},
show: function()
{
this.background.visible = true;
},
toSaveObject: function()
{
var obj = {};
obj.name = this.name;
obj.things = [];
for(var i = 0; i < this.things.length; i++)
{
obj.things[obj.things.length] = this.things[i].toSaveObject();
}
return obj;
},
loadFromObject: function(obj)
{
this.things = new Array();
for(var i = 0; i < obj.things.length; i++)
{
Adventurous.thingsMap[obj.things[i].name].loadFromObject(obj.things[i]);
this.things[this.things.length] = Adventurous.thingsMap[obj.things[i].name];
}
}
};