-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.js
More file actions
127 lines (102 loc) · 3.61 KB
/
Copy pathcell.js
File metadata and controls
127 lines (102 loc) · 3.61 KB
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
import { CellParser } from "./cellparser.js";
import { showMessage, sleep } from "./utils.js";
/**
* a cell in the map
*/
export class Cell {
isObstacle = false;
obj = undefined;
zone = undefined;
constructor(game, ix, iy, txt) {
if (txt == "player" || txt == "") return;
const x = ix * 32;
const y = iy * 32;
const arg = txt.split(" ");
if (arg[0] == "sign") {
this.obj = createObject(game.phaser, game.mapCellsPortion.obstacles, x, y, 'sign');
this.zone = createZone(game.phaser, x, y, this.obj.displayWidth+10, this.obj.displayHeight+10);
//const sign = game.obstacles.create(x, y, 'sign');
game.phaser.physics.add.overlap(game.player.obj, this.zone,
() => game.currentFunction = () => showMessage(txt.substr(5)));
}
else if (arg[0].startsWith("obj")) {
this.obj = createObject(game.phaser, game.mapCellsPortion.objects, x, y, arg[0]);
game.phaser.physics.add.overlap(game.player.obj, this.obj, () => {
game.phaser.sound.play('objectGet');
game.state[arg[0]] = true;
this.obj.destroy();
this.obj = undefined;
game.refresh();
});
}
else {
this.isObstacle = CellParser.isLabelObstacle(arg[0]);
this.obj = createObject(game.phaser, this.isObstacle ? game.mapCellsPortion.obstacles : game.mapCellsPortion.objects, x, y, arg[0]);
this.zone = createZone(game.phaser, x, y, this.obj.displayWidth+10, this.obj.displayHeight+10);
if (arg[1])
if (CellParser.isTunnel(arg[1])) {
game.phaser.physics.add.overlap(game.player.obj,
this.obj, () => game.takeTunnel(arg[1], { ix: ix, iy: iy }));
}
const funcWhenAction = async () => {
await game.script[arg[0]]();
game.refresh();
}
const funcWhenOn = game.script["on_" + arg[0]];
//if the action script is defined for that object
if (game.script[arg[0]]) {
game.phaser.physics.add.overlap(game.player.obj, this.zone, () => game.currentFunction = funcWhenAction);
}
if (funcWhenOn) {
game.phaser.physics.add.overlap(game.player.obj, this.obj, () => game.script["on_" + arg[0]]());
}
}
}
/**
* destroy the cell
*/
destroy() {
if (this.obj) {
this.obj.destroy();
this.obj = undefined;
}
if (this.zone) {
this.zone.destroy();
this.zone = undefined;
}
}
reload() {
}
}
/**
*
* @param {*} phaser
* @param {*} collection
* @param {*} x
* @param {*} y
* @param {*} name
* @returns a sprite whose image is name
*/
function createObject(phaser, collection, x, y, name) {
if (phaser.textures.exists(name)) return collection.create(x, y, name);
const obj = collection.create(x, y, "none");
phaser.load.image(name, `assets/${name}.png`)
phaser.load.once(Phaser.Loader.Events.COMPLETE, () => { obj.setTexture(name) });
obj.setBounce(1);
phaser.load.start();
return obj;
}
/**
*
* @param {*} phaser
* @param {*} x
* @param {*} y
* @returns a zone a bit larger than a cell (for overlaping tests ;)
*/
function createZone(phaser, x, y, w, h) {
const zone = phaser.add.zone(x, y).setSize(w, h);
phaser.physics.world.enable(zone);
zone.body.setAllowGravity(false);
zone.body.moves = false;
return zone;
}