BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / frog.js

frog.js

frog basic animation
timfa wsadhelios · pushed by timfa · 4 years ago
e5af8c64 History
frog.js 2.1 KiB Download Raw
1class Frog extends GameObject {
2
3 facing = {
4 left: [4,5,6],
5 right: [0,1,2]
6 };
7
8 speed = 200;
9
10 currentFacing = this.facing.right;
11 idle = true;
12
13 currentIndex = 0;
14 frame = 0;
15
16 constructor(x,y,sprite,layer) {
17 super(x,y,sprite,layer);
18 }
19
20 update(deltaTime)
21 {
22 super.update(deltaTime);
23 this.idle = true;
24
25 if(GameInput.isHeld(GameInput.keys.ArrowRight))
26 {
27 this.transform.position.x += this.speed * deltaTime;
28
29 if(this.currentFacing != this.facing.right) {
30 this.changeImageDirection();
31 this.currentFacing = this.facing.right;
32 }
33
34 CookieManager.setCookie("arrow", "right");
35 this.idle = false;
36 this.cycleImage(deltaTime);
37 }
38
39 if(GameInput.isHeld(GameInput.keys.ArrowLeft))
40 {
41 this.transform.position.x -= this.speed * deltaTime;
42
43 if(this.currentFacing != this.facing.left) {
44 this.changeImageDirection();
45 this.currentFacing = this.facing.left;
46 }
47
48 CookieManager.setCookie("arrow", "left")
49 this.idle = false;
50 this.cycleImage(deltaTime);
51 }
52
53 if(this.idle) {
54 this.currentIndex = 0;
55 this.resolveToIdleImage();
56 }
57 }
58
59 changeImageDirection() {
60 if(this.renderer.subImage > 3) {
61 this.renderer.subImage -= 4;
62 } else {
63 this.renderer.subImage += 4;
64 }
65 }
66
67 resolveToIdleImage() {
68 if(this.renderer.subImage > 3) {
69 if(this.renderer.subImage != 4) {
70 this.renderer.subImage = 4;
71 }
72 } else {
73 if(this.renderer.subImage != 0) {
74 this.renderer.subImage = 0;
75 }
76 }
77 }
78
79 cycleImage(deltaTime) {
80 this.frame+= deltaTime;
81
82 if(this.frame > .05) {
83 this.frame = 0;
84 this.currentIndex++;
85 this.renderer.subImage = this.currentFacing[this.currentIndex % this.currentFacing.length];
86 }
87 }
88}