BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

framework
timfa Tim · pushed by timfa · 4 years ago
eb480ae5 History
chao.js 6.4 KiB Download Raw
1class Chao extends GameObject
2{
3 speed = 40;
4
5 constructor(x, y, sprite, layer)
6 {
7 super(x, y, sprite, layer);
8 this.wasHeld = {}
9 this.collider.enabled = true;
10 }
11
12 onClick(btn)
13 {
14 console.log(btn);
15 }
16
17 anyHeld()
18 {
19 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
20 }
21
22 update(deltaTime)
23 {
24 super.update(deltaTime);
25 this.wasHeld = this.wasHeld || {}
26
27 if(this.wasHeld.right && !GameInput.isHeld(GameInput.keys.ArrowRight))
28 {
29 this.wasHeld.right = false;
30 if(!this.anyHeld())
31 this.renderer.setAnimation("standLeft", 0, .2);
32 }
33
34 if(this.wasHeld.left && !GameInput.isHeld(GameInput.keys.ArrowLeft))
35 {
36 this.wasHeld.left = false;
37 if(!this.anyHeld())
38 this.renderer.setAnimation("standLeft", 0, .2);
39 }
40
41 if(this.wasHeld.up && !GameInput.isHeld(GameInput.keys.ArrowUp))
42 {
43 this.wasHeld.up = false;
44 if(!this.anyHeld())
45 this.renderer.setAnimation("standUp", 0, .2);
46 }
47
48 if(this.wasHeld.down && !GameInput.isHeld(GameInput.keys.ArrowDown))
49 {
50 this.wasHeld.down = false;
51 if(!this.anyHeld())
52 this.renderer.setAnimation("standDown", 0, .2);
53 }
54
55 if(GameInput.isHeld(GameInput.keys.ArrowRight) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.down)
56 {
57 this.transform.position.x += this.speed * deltaTime;
58 CookieManager.setCookie("arrow", "right");
59
60 if(!this.wasHeld.right)
61 this.renderer.setAnimation("walkLeft", 0, .2);
62 this.renderer.mirrorX = true;
63 this.wasHeld.right = true;
64 }
65 else if(GameInput.isHeld(GameInput.keys.ArrowLeft) && !this.wasHeld.right && !this.wasHeld.up && !this.wasHeld.down)
66 {
67 this.transform.position.x -= this.speed * deltaTime;
68 CookieManager.setCookie("arrow", "left")
69
70 if(!this.wasHeld.left)
71 this.renderer.setAnimation("walkLeft", 0, .2);
72 this.renderer.mirrorX = false;
73 this.wasHeld.left = true;
74 }
75 else if(GameInput.isHeld(GameInput.keys.ArrowUp) && !this.wasHeld.left && !this.wasHeld.right && !this.wasHeld.down)
76 {
77 this.transform.position.y -= this.speed * deltaTime;
78 CookieManager.setCookie("arrow", "up")
79
80 if(!this.wasHeld.up)
81 this.renderer.setAnimation("walkUp", 0, .2);
82 this.renderer.mirrorX = false;
83 this.wasHeld.up = true;
84 }
85 else if(GameInput.isHeld(GameInput.keys.ArrowDown) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.right)
86 {
87 this.transform.position.y += this.speed * deltaTime;
88 CookieManager.setCookie("arrow", "down")
89
90 if(!this.wasHeld.down)
91 this.renderer.setAnimation("walkDown", 0, .2);
92 this.renderer.mirrorX = false;
93 this.wasHeld.down = true;
94 }
95 else
96 {
97 if(GameInput.isPressed(GameInput.keys["1"]))
98 {
99 this.renderer.setAnimation("tripDown", 0, .2)
100 }
101
102 if(GameInput.isPressed(GameInput.keys["2"]))
103 {
104 this.renderer.setAnimation("tripLeft", 0, .2)
105 }
106
107 if(GameInput.isPressed(GameInput.keys["3"]))
108 {
109 this.renderer.setAnimation("tripUp", 0, .2)
110 }
111
112 if(GameInput.isPressed(GameInput.keys["4"]))
113 {
114 this.renderer.setAnimation("sitDown", 0, .2)
115 }
116
117 if(GameInput.isPressed(GameInput.keys["5"]))
118 {
119 this.renderer.setAnimation("sitLeft", 0, .2)
120 }
121
122 if(GameInput.isPressed(GameInput.keys["6"]))
123 {
124 this.renderer.setAnimation("tantrum", 0, .2)
125 }
126
127 if(GameInput.isPressed(GameInput.keys["7"]))
128 {
129 this.renderer.setAnimation("swimStruggle", 0, .2)
130 }
131
132 if(GameInput.isPressed(GameInput.keys["8"]))
133 {
134 this.renderer.setAnimation("swim", 0, .2)
135 }
136
137 if(GameInput.isPressed(GameInput.keys["9"]))
138 {
139 this.renderer.setAnimation("eatStart", 0, .2)
140 }
141
142 if(GameInput.isPressed(GameInput.keys["0"]))
143 {
144 this.renderer.setAnimation("cheer", 0, .2)
145 }
146
147 if(GameInput.isPressed(GameInput.keys["q"]))
148 {
149 this.renderer.setAnimation("wave", 0, .2)
150 }
151
152 if(GameInput.isPressed(GameInput.keys["w"]))
153 {
154 this.renderer.setAnimation("hungry", 0, .2)
155 }
156
157 if(GameInput.isPressed(GameInput.keys["e"]))
158 {
159 this.renderer.setAnimation("no", 0, .2)
160 }
161
162 if(GameInput.isPressed(GameInput.keys["r"]))
163 {
164 this.renderer.setAnimation("think", 0, .2)
165 }
166
167 if(GameInput.isPressed(GameInput.keys["t"]))
168 {
169 this.renderer.setAnimation("happy", 0, .2)
170 }
171
172 if(GameInput.isPressed(GameInput.keys["y"]))
173 {
174 this.renderer.setAnimation("sad", 0, .2)
175 }
176
177 if(GameInput.isPressed(GameInput.keys["u"]))
178 {
179 this.renderer.setAnimation("sleep", 0, .2)
180 }
181
182 if(GameInput.isPressed(GameInput.keys["i"]))
183 {
184 this.renderer.setAnimation("eatBig", 0, .2)
185 }
186
187 if(GameInput.isPressed(GameInput.keys["o"]))
188 {
189 this.renderer.setAnimation("sneeze", 0, .2)
190 }
191
192 if(GameInput.isPressed(GameInput.keys["p"]))
193 {
194 this.renderer.setAnimation("cry", 0, .2)
195 }
196
197 if(GameInput.isPressed(GameInput.keys["["]))
198 {
199 this.renderer.setAnimation("think2", 0, .2)
200 }
201 }
202 }
203}