BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

chao.js 2.2 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.images.gem, 0.1, 45, 0, 4, 0, new vector(0, 0.1), 0, new vector(30, 30), new vector(50, 50), 2, 2.5, 0.5, 1, 0)
50 this.particler.start();
51 }
52
53 onClick(btn)
54 {
55 console.log(btn);
56 }
57
58 anyHeld()
59 {
60 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
61 }
62
63 update(deltaTime)
64 {
65 super.update(deltaTime);
66 this.particler.emissionAngle = Math.randomRange(10, 60)
67 this.particler.startFrame = Math.floor(Math.randomRange(0, this.scene.game.images.gem.subImages))
68 this.particler.update(deltaTime)
69
70 if(this.busyWithBehaviour)
71 this.currentBehaviour.update(deltaTime);
72 else
73 {
74 this.behaviourTimer -= deltaTime;
75
76 if(this.behaviourTimer <= 0)
77 {
78 this.currentBehaviour = this.behaviourList.random();
79 this.currentBehaviour.startBehaviour();
80 }
81 }
82 }
83
84 draw(context)
85 {
86 super.draw(context);
87 this.particler.draw(this.scene)
88 }
89}