BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

wandering
timfa Tim · pushed by timfa · 4 years ago
656b954c History
chao.js 5.3 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 = 10;
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 this.particler = new ParticleEmitter(new vector(100, 100), this.scene.game.images.gem, 0.1, 45, 0, 25, 0, new vector(0, 50), 0, new vector(30, 30), new vector(50, 50), 2, 2.5, 0.5, 1, 0)
60 this.particler.friction = new vector(50, 50);
61 this.particler.start();
62 }
63
64 onClick(btn)
65 {
66 console.log(btn);
67 }
68
69 anyHeld()
70 {
71 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
72 }
73
74 walkToPosition(position)
75 {
76 console.log("Walking towards", position)
77 this.targetDestination = position.getCopy();
78 this.prevPosition = this.transform.position.getCopy();
79 this.hBeforeV = Math.random() > 0.5;
80 }
81
82 update(deltaTime)
83 {
84 super.update(deltaTime);
85 this.particler.emissionAngle = Math.randomRange(10, 60)
86 this.particler.startFrame = Math.floor(Math.randomRange(0, this.scene.game.images.gem.subImages))
87 this.particler.update(deltaTime)
88
89 if(GameInput.isPressed(GameInput.keys.a))
90 {
91 this.walkToPosition(GameInput.mousePosition.getCopy());
92 }
93
94 if(this.busyWithBehaviour)
95 this.currentBehaviour.update(deltaTime);
96 else
97 {
98 if(!this.walking)
99 {
100 this.behaviourTimer -= deltaTime;
101
102 if(this.behaviourTimer <= 0)
103 {
104 this.currentBehaviour = this.behaviourList.random();
105 console.log("Starting Behaviour '" + this.currentBehaviour.constructor.name + "'")
106 this.currentBehaviour.startBehaviour();
107 }
108 }
109 }
110
111 if(this.walking)
112 {
113 let halfTotalDistanceH = Math.abs(this.prevPosition.x - this.targetDestination.x) / 2
114 let halfTotalDistanceV = Math.abs(this.prevPosition.y - this.targetDestination.y) / 2
115
116 let currentDistanceH = Math.abs(this.transform.position.x - this.targetDestination.x)
117 let currentDistanceV = Math.abs(this.transform.position.y - this.targetDestination.y)
118
119 let hDir = -Math.sign(this.transform.position.x - this.targetDestination.x)
120 let vDir = -Math.sign(this.transform.position.y - this.targetDestination.y)
121
122 if(this.hBeforeV)
123 {
124 if(currentDistanceH > halfTotalDistanceH || currentDistanceV < 1)
125 {
126 this.renderer.mirrorX = hDir > 0;
127 this.transform.velocity.x = this.speed * hDir;
128 this.transform.velocity.y = 0;
129 this.renderer.setAnimation("walkLeft")
130 }
131 else
132 {
133 this.transform.velocity.x = 0;
134 this.transform.velocity.y = this.speed * vDir;
135 this.renderer.setAnimation(vDir < 0? "walkUp" : "walkDown")
136 }
137 }
138 else
139 {
140 if(currentDistanceV > halfTotalDistanceV || currentDistanceH < 1)
141 {
142 this.transform.velocity.x = 0;
143 this.transform.velocity.y = this.speed * vDir;
144 this.renderer.setAnimation(vDir < 0? "walkUp" : "walkDown")
145 }
146 else
147 {
148 this.renderer.mirrorX = hDir > 0;
149 this.transform.velocity.x = this.speed * hDir;
150 this.transform.velocity.y = 0;
151 this.renderer.setAnimation("walkLeft")
152 }
153 }
154 }
155 else if(this.renderer.currentAnimationName && this.renderer.currentAnimationName.indexOf("walk") >= 0)
156 {
157 this.renderer.setAnimation(this.renderer.currentAnimationName.replace("walk", "stand"))
158
159 this.transform.velocity.x = 0
160 this.transform.velocity.y = 0;
161 }
162 }
163
164 draw(context)
165 {
166 super.draw(context);
167 this.particler.draw(this.scene)
168 }
169}