BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / worldItems / base / 1-worldItem.js

1-worldItem.js

fbb32e05 History
1-worldItem.js 1.6 KiB Download Raw
1class WorldItem extends GameObject
2{
3 wi_wasHeld = false;
4 wi_isHeld = false;
5 grabbable = true;
6 wi_grabbed = false;
7 data = {};
8
9 /**
10 * IMPORTANT: Any extra parameters AFTER layer in extending classes MUST be optional!
11 */
12 constructor (x, y, sprite, layer)
13 {
14 super(x, y, sprite, layer);
15 WorldObjectTracker.Track(this);
16 this.collider.enabled = true;
17 }
18
19 onClick (btn)
20 {
21 if (this.grabbable)
22 this.wi_grabbed = true;
23 }
24
25 onMouseHeld (btn)
26 {
27 }
28
29 update (dt)
30 {
31 super.update(dt);
32
33 let anyHeld = GameInput.mouseHeld();
34
35 if (this.wi_grabbed && !(anyHeld.left || anyHeld.right || anyHeld.middle))
36 {
37 WorldObjectTracker.Save();
38 this.wi_grabbed = false;
39 }
40
41 if (this.wi_grabbed)
42 this.transform.position = GameInput.mousePosition;
43
44 if (this.transform.position.x - this.transform.size.x / 2 < 0)
45 this.transform.position.x++;
46
47 if (this.transform.position.x + this.transform.size.x / 2 > 704)
48 this.transform.position.x--;
49
50 if (this.transform.position.y - this.transform.size.y / 2 < 0)
51 this.transform.position.y++;
52
53 if (this.transform.position.y + this.transform.size.x / 2 > 640)
54 this.transform.position.y--;
55
56 }
57
58 onSaveRestored ()
59 {
60
61 }
62
63 onCollision (o2)
64 {
65 if (o2 instanceof WorldItem)
66 {
67 this.transform.position = this.transform.position.add(this.transform.position.subtract(o2.transform.position).normalize().stretch(o2.collider.widestDiagonal * 0.05));
68 }
69 }
70}