BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frog.js

frog.js

frog update
timfa wsadhelios · pushed by timfa · 4 years ago
a31810aa History
frog.js 7.0 KiB Download Raw
1class Frog extends GameObject {
2
3 speed = 100;
4
5 facing =
6 {
7 left: -.8,
8 right:.8
9 };
10
11 currentFacing = this.facing.right;
12 idle = true;
13
14 tongueVector = null;
15 tongueVectorBounce = null;
16
17 tongueVectorBounceOrigin = null;
18
19 tongueLength = 0;
20 tongueLengthBounce = 0;
21 retracting = false;
22
23 frog_tongue_base = null;
24 frog_tongue_tip = null;
25
26 constructor(x,y,sprite,layer)
27 {
28 super(x,y,sprite,layer);
29 this.collider.enabled = true;
30 }
31
32 addedToScene()
33 {
34 this.frog_tongue_base = this.scene.game.images.frog_tongue_base;
35 this.frog_tongue_tip = this.scene.game.images.frog_tongue_tip;
36 }
37
38 update(deltaTime)
39 {
40 super.update(deltaTime);
41
42 this.idle = true;
43
44 if(this.tongue_retracted && !this.retracting)
45 {
46 if(! (GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft)))
47 {
48 if(GameInput.isHeld(GameInput.keys.ArrowRight))
49 {
50 this.arrowCounter++;
51 if(this.canMove(this.transform.position.x + this.speed * deltaTime)) {
52 this.transform.position.x += this.speed * deltaTime;
53 }
54
55 if(this.currentFacing != this.facing.right)
56 {
57 this.renderer.mirrorX = !this.renderer.mirrorX;
58 this.currentFacing = this.facing.right;
59 }
60
61 CookieManager.setCookie("arrow", "right");
62 this.idle = false;
63 this.renderer.setAnimation("walk", 0, .2);
64 }
65
66 if(GameInput.isHeld(GameInput.keys.ArrowLeft))
67 {
68 this.arrowCounter++;
69 if(this.canMove(this.transform.position.x - this.speed * deltaTime)) {
70 this.transform.position.x -= this.speed * deltaTime;
71 }
72
73 if(this.currentFacing != this.facing.left)
74 {
75 this.renderer.mirrorX = !this.renderer.mirrorX;
76 this.currentFacing = this.facing.left;
77 }
78
79 CookieManager.setCookie("arrow", "left")
80 this.idle = false;
81 this.renderer.setAnimation("walk", 0, .2);
82 }
83 }
84 }
85
86 if(GameInput.isHeld(GameInput.keys[" "]) && !this.retracting)
87 {
88 this.renderer.setSubImage(3);
89 this.idle = false;
90 this.tongueLength += deltaTime * this.tongue_speed;
91 }
92 else
93 {
94 if(!this.tongue_retracted)
95 {
96 if(this.tongueLengthBounce > 0) {
97 this.tongueLengthBounce -= deltaTime * this.tongue_speed * 2;
98 }
99 this.tongueLength -= deltaTime * this.tongue_speed * 2;
100 this.retracting = true;
101 }
102 else
103 {
104 this.retracting = false;
105 }
106 }
107
108 if(this.idle && this.tongue_retracted && !this.retracting)
109 {
110 this.renderer.setAnimation("walk", 0, -1);
111 }
112
113 if(!this.tongue_retracted && !this.retracting)
114 {
115 this.doTongueStuff(deltaTime);
116 }
117 }
118
119 get tongue_retracted() {
120 return this.tongueLength <= 0;
121 }
122
123 get tongue_speed() {
124 return 150 * this.scene.gameSpeed;
125 }
126
127 get opposite_facing() {
128 return this.currentFacing == this.facing.left ? this.facing.right : this.facing.left;
129 }
130
131 canMove(nextPos)
132 {
133 return nextPos + this.transform.size.x / 2 < this.scene.real_size.x && nextPos - this.transform.size.x / 2 > 0
134 }
135
136 doTongueStuff(deltaTime)
137 {
138 let dir = new vector(this.currentFacing, -1);
139 this.tongueVector = this.transform.position.add(dir.stretch(this.tongueLength));
140
141 if(this.tongueVector.y <= 0 || this.tongueVector.y >= this.scene.size.y) {
142 this.retracting = true;
143 return;
144 }
145
146 if(this.tongueVector.x <= 0 || this.tongueVector.x >= this.scene.size.x )
147 {
148 if(!(this.tongueLengthBounce > 1))
149 this.tongueVectorBounceOrigin = this.tongueVector.getCopy();
150
151 this.tongueLengthBounce += deltaTime * this.tongue_speed;
152 dir = new vector(this.opposite_facing, -1);
153 this.tongueVectorBounce = this.tongueVectorBounceOrigin.add(dir.stretch(this.tongueLengthBounce));
154
155 if(this.tongueVectorBounce.y <= 0 || this.tongueVectorBounce.y >= this.scene.size.y) {
156 this.retracting = true;
157 return;
158 }
159 }
160 }
161
162 draw(context)
163 {
164 super.draw(context);
165
166 if(!this.tongue_retracted) {
167
168 let dir = new vector(this.currentFacing, -1);
169
170 this.tongueVector = this.transform.position.add(dir.stretch(this.tongueLength));
171 let xOffset = this.currentFacing > 0 ? 5 : -5;
172
173 this.frog_tongue_tip.draw(this.scene,
174 0, 0,
175 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
176 this.tongueVector.x + xOffset, this.tongueVector.y,
177 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
178 dir.toAngle() -90, 0, 0, 1)
179
180 this.frog_tongue_base.draw(
181 this.scene,
182 0, 0,
183 this.frog_tongue_base.width, this.frog_tongue_base.height,
184 (this.transform.position.x + this.tongueVector.x) / 2 + xOffset, (this.transform.position.y + this.tongueVector.y) / 2,
185 this.frog_tongue_base.width * (this.tongueLength / 12.5), this.frog_tongue_base.height,
186 dir.toAngle() -90, 0, 0, 1)
187
188 if(this.tongueLengthBounce > 0) {
189 dir = new vector(this.opposite_facing, -1);
190 this.tongueVectorBounce = this.tongueVectorBounceOrigin.add(dir.stretch(this.tongueLengthBounce));
191 xOffset = this.opposite_facing > 0 ? 5 : -5;
192
193 this.frog_tongue_tip.draw(this.scene,
194 0, 0,
195 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
196 this.tongueVectorBounce.x + xOffset, this.tongueVectorBounce.y,
197 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
198 dir.toAngle() -90, 0, 0, 1)
199
200 this.frog_tongue_base.draw(
201 this.scene,
202 0, 0,
203 this.frog_tongue_base.width, this.frog_tongue_base.height,
204 (this.tongueVectorBounceOrigin.x + this.tongueVectorBounce.x) / 2 + xOffset, (this.tongueVectorBounceOrigin.y + this.tongueVectorBounce.y) / 2,
205 this.frog_tongue_base.width * (this.tongueLengthBounce / 12.5), this.frog_tongue_base.height,
206 dir.toAngle() -90, 0, 0, 1)
207 }
208 }
209 }
210}