BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

chao.js 4.8 KiB Download Raw
1class Chao extends GameObject
2{
3 speed = 40;
4 scenePadding = 50;
5 prevPosition = null;
6 targetDestination = null;
7 hBeforeV = false;
8
9 minTimeBetweenBehaviour = 3; //in seconds
10 maxTimeBetweenbehaviour = 10; // in seconds
11 behaviourTimer = 3;
12
13 behaviourList = [];
14 currentBehaviour = null;
15
16 particler = null;
17
18 static Instance;
19
20 get busyWithBehaviour ()
21 {
22 return this.currentBehaviour != null;
23 }
24
25 get walking ()
26 {
27 return this.transform.position.distanceTo(this.targetDestination) > 5;
28 }
29
30 constructor (x, y, sprite, layer)
31 {
32 super(x, y, sprite, layer);
33 this.wasHeld = {};
34 this.collider.enabled = true;
35 Chao.Instance = this;
36 this.targetDestination = new vector(x, y);
37 }
38
39 registerBehaviour (behaviour)
40 {
41 this.behaviourList.push(behaviour);
42 }
43
44 endBehaviour ()
45 {
46 console.log("Ending Behaviour '" + this.currentBehaviour.constructor.name + "'");
47 this.currentBehaviour = null;
48 this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour);
49 }
50
51 addedToScene ()
52 {
53 this.renderer.sprite = this.scene.game.chaoImages.random();
54
55 this.renderer.defaultAnimationSpeed = 0.2;
56 this.registerBehaviour(new BehaviourSneeze());
57 this.registerBehaviour(new BehaviourWander());
58
59 }
60
61 onClick (btn)
62 {
63 console.log(btn);
64 }
65
66 anyHeld ()
67 {
68 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
69 }
70
71 walkToPosition (position)
72 {
73 console.log("Walking towards", position);
74 this.targetDestination = position.getCopy();
75 this.prevPosition = this.transform.position.getCopy();
76 this.hBeforeV = Math.random() > 0.5;
77 }
78
79 update (deltaTime)
80 {
81 super.update(deltaTime);
82 if (GameInput.isPressed(GameInput.keys.a))
83 {
84 this.walkToPosition(GameInput.mousePosition.getCopy());
85 }
86
87 if (this.busyWithBehaviour)
88 this.currentBehaviour.update(deltaTime);
89 else
90 {
91 if (!this.walking)
92 {
93 this.behaviourTimer -= deltaTime;
94
95 if (this.behaviourTimer <= 0)
96 {
97 this.currentBehaviour = this.behaviourList.random();
98 console.log("Starting Behaviour '" + this.currentBehaviour.constructor.name + "'");
99 this.currentBehaviour.startBehaviour();
100 }
101 }
102 }
103
104 if (this.walking)
105 {
106 let halfTotalDistanceH = Math.abs(this.prevPosition.x - this.targetDestination.x) / 2;
107 let halfTotalDistanceV = Math.abs(this.prevPosition.y - this.targetDestination.y) / 2;
108
109 let currentDistanceH = Math.abs(this.transform.position.x - this.targetDestination.x);
110 let currentDistanceV = Math.abs(this.transform.position.y - this.targetDestination.y);
111
112 let hDir = -Math.sign(this.transform.position.x - this.targetDestination.x);
113 let vDir = -Math.sign(this.transform.position.y - this.targetDestination.y);
114
115 if (this.hBeforeV)
116 {
117 if (currentDistanceH > halfTotalDistanceH || currentDistanceV < 1)
118 {
119 this.renderer.mirrorX = hDir > 0;
120 this.transform.velocity.x = this.speed * hDir;
121 this.transform.velocity.y = 0;
122 this.renderer.setAnimation("walkLeft");
123 }
124 else
125 {
126 this.transform.velocity.x = 0;
127 this.transform.velocity.y = this.speed * vDir;
128 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
129 }
130 }
131 else
132 {
133 if (currentDistanceV > halfTotalDistanceV || currentDistanceH < 1)
134 {
135 this.transform.velocity.x = 0;
136 this.transform.velocity.y = this.speed * vDir;
137 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
138 }
139 else
140 {
141 this.renderer.mirrorX = hDir > 0;
142 this.transform.velocity.x = this.speed * hDir;
143 this.transform.velocity.y = 0;
144 this.renderer.setAnimation("walkLeft");
145 }
146 }
147 }
148 else if (this.renderer.currentAnimationName && this.renderer.currentAnimationName.indexOf("walk") >= 0)
149 {
150 this.renderer.setAnimation(this.renderer.currentAnimationName.replace("walk", "stand"));
151
152 this.transform.velocity.x = 0;
153 this.transform.velocity.y = 0;
154 }
155 }
156
157 draw (context)
158 {
159 super.draw(context);
160 }
161}