BlueGit

blue-star speeltuin

better dragging, bounds checking

timfa Tim · pushed by timfa

Authored 2022-10-22 · 4 years ago

Hash
fbb32e05d4e3985764bdd02e03b61f3a845623a3
Parent
a457be13
1 file changed +35 −8
src/objects/worldItems/base/1-worldItem.js +35 −8
@@ -3,6 +3,7 @@ class WorldItem extends GameObject
3 3 wi_wasHeld = false;
4 4 wi_isHeld = false;
5 5 grabbable = true;
6 wi_grabbed = false;
6 7 data = {};
7 8
8 9 /**
@@ -12,32 +13,58 @@ class WorldItem extends GameObject
12 13 {
13 14 super(x, y, sprite, layer);
14 15 WorldObjectTracker.Track(this);
16 this.collider.enabled = true;
15 17 }
16 18
17 onMouseHeld (btn)
19 onClick (btn)
18 20 {
19 this.wi_isHeld = true;
20
21 21 if (this.grabbable)
22 this.transform.position = GameInput.mousePosition;
22 this.wi_grabbed = true;
23 }
24
25 onMouseHeld (btn)
26 {
23 27 }
24 28
25 29 update (dt)
26 30 {
27 31 super.update(dt);
28 32
29 if (this.wi_wasHeld && !this.wi_isHeld)
33 let anyHeld = GameInput.mouseHeld();
34
35 if (this.wi_grabbed && !(anyHeld.left || anyHeld.right || anyHeld.middle))
30 36 {
31 37 WorldObjectTracker.Save();
32 this.wi_wasHeld = false;
38 this.wi_grabbed = false;
33 39 }
34 40
35 this.wi_wasHeld = this.wi_isHeld;
36 this.wi_isHeld = false;
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
37 56 }
38 57
39 58 onSaveRestored ()
40 59 {
41 60
42 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 }
43 70 }
@@ -3,6 +3,7 @@ class WorldItem extends GameObject
3 wi_wasHeld = false;
3 wi_wasHeld = false;
4 wi_isHeld = false;
4 wi_isHeld = false;
5 grabbable = true;
5 grabbable = true;
6 wi_grabbed = false;
6 data = {};
7 data = {};
7
8
8 /**
9 /**
@@ -12,32 +13,58 @@ class WorldItem extends GameObject
12 {
13 {
13 super(x, y, sprite, layer);
14 super(x, y, sprite, layer);
14 WorldObjectTracker.Track(this);
15 WorldObjectTracker.Track(this);
16 this.collider.enabled = true;
15 }
17 }
16
18
17 onMouseHeld (btn)
19 onClick (btn)
18 {
20 {
19 this.wi_isHeld = true;
20
21 if (this.grabbable)
21 if (this.grabbable)
22 this.transform.position = GameInput.mousePosition;
22 this.wi_grabbed = true;
23 }
24
25 onMouseHeld (btn)
26 {
23 }
27 }
24
28
25 update (dt)
29 update (dt)
26 {
30 {
27 super.update(dt);
31 super.update(dt);
28
32
29 if (this.wi_wasHeld && !this.wi_isHeld)
33 let anyHeld = GameInput.mouseHeld();
34
35 if (this.wi_grabbed && !(anyHeld.left || anyHeld.right || anyHeld.middle))
30 {
36 {
31 WorldObjectTracker.Save();
37 WorldObjectTracker.Save();
32 this.wi_wasHeld = false;
38 this.wi_grabbed = false;
33 }
39 }
34
40
35 this.wi_wasHeld = this.wi_isHeld;
41 if (this.wi_grabbed)
36 this.wi_isHeld = false;
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
37 }
56 }
38
57
39 onSaveRestored ()
58 onSaveRestored ()
40 {
59 {
41
60
42 }
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 }
43}
70}