-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaddle.js
77 lines (58 loc) · 1.41 KB
/
paddle.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
import {width as screenWidth, bounds} from './data.js';
import {partial_collision} from './collision.js';
import {Graphics} from './pixi.min.mjs';
class PaddleEvents {
onMove = [];
onBoundsCollide = [];
call(listener, event) {
for(let func of this[listener]) {
func(event);
}
}
}
class Paddle extends Graphics {
constructor() {
super();
this.beginFill(0xff1f1f);
this.drawRect(0,0,75,15);
this.listener = new PaddleEvents();
this.x = (screenWidth - this.width) / 2;
this.y = 10;
this.dx = 0;
this.dy = 0;
this.speed = 6;
this.obstacle = bounds.arena;
}
#move() {
let collide = partial_collision(this, this.obstacle);
if(collide['collision']) {
let params = {};
params.x = this.obstacle.x;
if(this.dx > 0)
params.x += this.obstacle.width - this.width;
let event = {'event': params, 'object': this, 'canceled': false};
this.listener.call("onBoundsCollide", event);
if(!event['canceled'])
this.x = params.x;
return;
}
let params = {};
params.x = this.x + this.dx;
params.dx = this.dx;
let event = {'event': params, 'object': this, 'canceled': false};
this.listener.call("onMove", event);
if(!event['canceled']) {
for(let field in params)
this[field] = params[field];
}
}
right() {
this.dx = this.speed;
this.#move();
}
left() {
this.dx = -1 * this.speed;
this.#move();
}
}
export default Paddle;