-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.js
132 lines (117 loc) · 2.91 KB
/
Block.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
/**
* @constructor
* @param {PIXI.Stage} stage
* @param {Game} game
* @param {Level} level
* @param {Vec2} tileCoords
* @param {number} tileSize
*/
function Block(stage, game, level, tileCoords, tileSize) {
this.stage = stage;
this.game = game;
this.level = level;
this.loc = this.level.getTileLoc(tileCoords);
this.texture = PIXI.Texture.fromImage('assets/block.png');
this.sprite = new PIXI.Sprite(this.texture);
this.sprite.position.x = this.loc.x;
this.sprite.position.y = this.loc.y;
this.sprite.width = tileSize;
this.sprite.height = tileSize;
this.stage.addChild(this.sprite);
this.vel = new Vec2(0, 0);
this.speed = 0.2;
this.moving = false;
}
/**
* Push the Block from a location
*
* @param {Vec2} loc
*/
Block.prototype.push = function(loc) {
var diff = this.loc.sub(loc);
this.vel = new Vec2(0, 0);
if (Math.abs(diff.x) > Math.abs(diff.y)) {
if (diff.x > 0) {
this.vel.x = this.speed;
} else {
this.vel.x = -this.speed;
}
} else {
if (diff.y > 0) {
this.vel.y = this.speed;
} else {
this.vel.y = -this.speed;
}
}
var tileCoords = this.level.getTileCoords(this.loc);
this.level.map.tileMap[tileCoords.x][tileCoords.y] = BlockType.EMPTY;
this.moving = true;
};
/**
* Update the Block's movement if necessary
*
* @param {number} dt
*/
Block.prototype.update = function(dt) {
if (!this.moving) {
return;
}
var vel = this.vel.mul(dt);
var collidedBlock = this.level.getCollision(this.loc, vel);
if (BlockType.isCollision(collidedBlock)) {
this.moving = false;
var tileCoords = this.level.getTileCoordsRounded(this.loc);
if (this.level.map.tileMap[tileCoords.x][tileCoords.y] === BlockType.GOAL) {
this.game.win();
return;
}
this.level.map.tileMap[tileCoords.x][tileCoords.y] = BlockType.BLOCK;
this.loc = this.level.getTileLoc(tileCoords);
} else {
this.loc = this.loc.add(vel);
}
this.sprite.position.x = this.loc.x;
this.sprite.position.y = this.loc.y;
};
/**
* Remove the block's sprite from the stage
*/
Block.prototype.remove = function() {
this.stage.removeChild(this.sprite);
};
var BlockType = {
EMPTY: ' ',
ANY: '?',
WALL: 'x',
BLOCK: 'B',
GOAL: 'G',
/**
* Whether a block represents a collision
*
* @param {String} block
* @return {boolean}
*/
isCollision: function(block) {
return block !== BlockType.EMPTY && block !== BlockType.GOAL;
},
/**
* Union the two blocks, effectively blockA || blockB if they were
* booleans
*
* @param {String} blockA
* @param {String} blockB
* @return {String}
*/
orCollision: function(blockA, blockB) {
if (typeof(blockA) === 'undefined') {
throw new Error('blockA undefined');
}
if (typeof(blockB) === 'undefined') {
throw new Error('blockB undefined');
}
if (BlockType.isCollision(blockA)) {
return blockA;
}
return blockB;
}
};