| 1 | class 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 | |
| 50 | onClick(btn) |
| 51 | { |
| 52 | console.log(btn); |
| 53 | } |
| 54 | |
| 55 | anyHeld() |
| 56 | { |
| 57 | return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down; |
| 58 | } |
| 59 | |
| 60 | update(deltaTime) |
| 61 | { |
| 62 | super.update(deltaTime); |
| 63 | |
| 64 | if(this.busyWithBehaviour) |
| 65 | this.currentBehaviour.update(deltaTime); |
| 66 | else |
| 67 | { |
| 68 | this.behaviourTimer -= deltaTime; |
| 69 | |
| 70 | if(this.behaviourTimer <= 0) |
| 71 | { |
| 72 | this.currentBehaviour = this.behaviourList.random(); |
| 73 | this.currentBehaviour.startBehaviour(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | draw(context) |
| 79 | { |
| 80 | super.draw(context); |
| 81 | } |
| 82 | } |