BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

chao.js 1.9 KiB Download Raw
1class Chao extends GameObject
2{
3 speed = 40;
4 scenePadding = 50;
5 targetDestination = null;
6
7 minTimeBetweenBehaviour = 3; //in seconds
8 maxTimeBetweenbehaviour = 10; // in seconds
9 behaviourTimer = 10;
10
11 behaviourList = [];
12 currentBehaviour = null;
13
14 particler = null;
15
16 static Instance;
17
18 get busyWithBehaviour()
19 {
20 return this.currentBehaviour != null;
21 }
22
23 constructor(x, y, sprite, layer)
24 {
25 super(x, y, sprite, layer);
26 this.wasHeld = {}
27 this.collider.enabled = true;
28 Chao.Instance = this;
29 }
30
31 registerBehaviour(behaviour)
32 {
33 this.behaviourList.push(behaviour);
34 }
35
36 endBehaviour()
37 {
38 this.currentBehaviour = null;
39 this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour);
40 }
41
42 addedToScene()
43 {
44 this.renderer.sprite = this.scene.game.chaoImages.random();
45
46 this.renderer.defaultAnimationSpeed = 0.2;
47 this.registerBehaviour(new BehaviourSneeze());
48
49 this.particler = new ParticleEmitter(new vector(100, 100), this.scene.game.gem, 0.1, 0)
50 }
51
52 onClick(btn)
53 {
54 console.log(btn);
55 }
56
57 anyHeld()
58 {
59 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
60 }
61
62 update(deltaTime)
63 {
64 super.update(deltaTime);
65 this.particler.update(deltaTime)
66
67 if(this.busyWithBehaviour)
68 this.currentBehaviour.update(deltaTime);
69 else
70 {
71 this.behaviourTimer -= deltaTime;
72
73 if(this.behaviourTimer <= 0)
74 {
75 this.currentBehaviour = this.behaviourList.random();
76 this.currentBehaviour.startBehaviour();
77 }
78 }
79 }
80
81 draw(context)
82 {
83 super.draw(context);
84 this.particler.draw(this.scene)
85 }
86}