| 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 | static Instance; |
| 15 | |
| 16 | get busyWithBehaviour() |
| 17 | { |
| 18 | return this.currentBehaviour != null; |
| 19 | } |
| 20 | |
| 21 | constructor(x, y, sprite, layer) |
| 22 | { |
| 23 | super(x, y, sprite, layer); |
| 24 | this.wasHeld = {} |
| 25 | this.collider.enabled = true; |
| 26 | Chao.Instance = this; |
| 27 | } |
| 28 | |
| 29 | registerBehaviour(behaviour) |
| 30 | { |
| 31 | this.behaviourList.push(behaviour); |
| 32 | } |
| 33 | |
| 34 | endBehaviour() |
| 35 | { |
| 36 | this.currentBehaviour = null; |
| 37 | this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour); |
| 38 | } |
| 39 | |
| 40 | addedToScene() |
| 41 | { |
| 42 | this.renderer.sprite = this.scene.game.chaoImages.random(); |
| 43 | |
| 44 | this.renderer.defaultAnimationSpeed = 0.2; |
| 45 | this.registerBehaviour(new BehaviourSneeze()); |
| 46 | } |
| 47 | |
| 48 | onClick(btn) |
| 49 | { |
| 50 | console.log(btn); |
| 51 | } |
| 52 | |
| 53 | anyHeld() |
| 54 | { |
| 55 | return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down; |
| 56 | } |
| 57 | |
| 58 | update(deltaTime) |
| 59 | { |
| 60 | super.update(deltaTime); |
| 61 | |
| 62 | if(this.busyWithBehaviour) |
| 63 | this.currentBehaviour.update(deltaTime); |
| 64 | else |
| 65 | { |
| 66 | this.behaviourTimer -= deltaTime; |
| 67 | |
| 68 | if(this.behaviourTimer <= 0) |
| 69 | { |
| 70 | this.currentBehaviour = this.behaviourList.random(); |
| 71 | this.currentBehaviour.startBehaviour(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |