| 1 | class 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 = 3; |
| 12 | |
| 13 | behaviourList = []; |
| 14 | currentBehaviour = null; |
| 15 | |
| 16 | particler = null; |
| 17 | |
| 18 | mood = |
| 19 | { //all values range from 0 to 1 |
| 20 | happy: 0.5, |
| 21 | angry: 0.5, |
| 22 | hungry: 0.5, |
| 23 | sad: 0.5, |
| 24 | curious: 0.5 |
| 25 | }; |
| 26 | |
| 27 | static Instance; |
| 28 | |
| 29 | get busyWithBehaviour () |
| 30 | { |
| 31 | return this.currentBehaviour != null; |
| 32 | } |
| 33 | |
| 34 | get walking () |
| 35 | { |
| 36 | return this.transform.position.distanceTo(this.targetDestination) > 5; |
| 37 | } |
| 38 | |
| 39 | constructor (x, y, sprite, layer) |
| 40 | { |
| 41 | super(x, y, sprite, layer); |
| 42 | this.wasHeld = {}; |
| 43 | this.collider.enabled = true; |
| 44 | Chao.Instance = this; |
| 45 | this.targetDestination = new vector(x, y); |
| 46 | } |
| 47 | |
| 48 | registerBehaviour (behaviour) |
| 49 | { |
| 50 | this.behaviourList.push(behaviour); |
| 51 | } |
| 52 | |
| 53 | endBehaviour () |
| 54 | { |
| 55 | console.log("Ending Behaviour '" + this.currentBehaviour.constructor.name + "'"); |
| 56 | this.currentBehaviour = null; |
| 57 | this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour); |
| 58 | } |
| 59 | |
| 60 | addedToScene () |
| 61 | { |
| 62 | let savedSprite = CookieManager.getCookie("chaoType"); |
| 63 | |
| 64 | if (savedSprite.length <= 0) |
| 65 | { |
| 66 | this.renderer.sprite = this.scene.game.chaoImages.random(); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | this.renderer.sprite = game.images[savedSprite]; |
| 71 | } |
| 72 | |
| 73 | CookieManager.setCookie("chaoType", this.renderer.sprite.name); |
| 74 | |
| 75 | this.renderer.defaultAnimationSpeed = 0.2; |
| 76 | this.registerBehaviour(new BehaviourEmote()); |
| 77 | this.registerBehaviour(new BehaviourWander()); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | onClick (btn) |
| 82 | { |
| 83 | console.log(btn); |
| 84 | } |
| 85 | |
| 86 | anyHeld () |
| 87 | { |
| 88 | return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down; |
| 89 | } |
| 90 | |
| 91 | walkToPosition (position) |
| 92 | { |
| 93 | console.log("Walking towards", position); |
| 94 | this.targetDestination = position.getCopy(); |
| 95 | this.prevPosition = this.transform.position.getCopy(); |
| 96 | this.hBeforeV = Math.random() > 0.5; |
| 97 | } |
| 98 | |
| 99 | update (deltaTime) |
| 100 | { |
| 101 | super.update(deltaTime); |
| 102 | |
| 103 | if (this.busyWithBehaviour) |
| 104 | this.currentBehaviour.update(deltaTime); |
| 105 | else |
| 106 | { |
| 107 | if (!this.walking) |
| 108 | { |
| 109 | this.behaviourTimer -= deltaTime; |
| 110 | |
| 111 | if (this.behaviourTimer <= 0) |
| 112 | { |
| 113 | this.currentBehaviour = this.behaviourList.random(); |
| 114 | console.log("Starting Behaviour '" + this.currentBehaviour.constructor.name + "'"); |
| 115 | this.currentBehaviour.startBehaviour(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (this.walking) |
| 121 | { |
| 122 | let halfTotalDistanceH = Math.abs(this.prevPosition.x - this.targetDestination.x) / 2; |
| 123 | let halfTotalDistanceV = Math.abs(this.prevPosition.y - this.targetDestination.y) / 2; |
| 124 | |
| 125 | let currentDistanceH = Math.abs(this.transform.position.x - this.targetDestination.x); |
| 126 | let currentDistanceV = Math.abs(this.transform.position.y - this.targetDestination.y); |
| 127 | |
| 128 | let hDir = -Math.sign(this.transform.position.x - this.targetDestination.x); |
| 129 | let vDir = -Math.sign(this.transform.position.y - this.targetDestination.y); |
| 130 | |
| 131 | if (this.hBeforeV) |
| 132 | { |
| 133 | if (currentDistanceH > halfTotalDistanceH || currentDistanceV < 1) |
| 134 | { |
| 135 | this.renderer.mirrorX = hDir > 0; |
| 136 | this.transform.velocity.x = this.speed * hDir; |
| 137 | this.transform.velocity.y = 0; |
| 138 | this.renderer.setAnimation("walkLeft"); |
| 139 | } |
| 140 | else |
| 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 | } |
| 147 | else |
| 148 | { |
| 149 | if (currentDistanceV > halfTotalDistanceV || currentDistanceH < 1) |
| 150 | { |
| 151 | this.transform.velocity.x = 0; |
| 152 | this.transform.velocity.y = this.speed * vDir; |
| 153 | this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown"); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | this.renderer.mirrorX = hDir > 0; |
| 158 | this.transform.velocity.x = this.speed * hDir; |
| 159 | this.transform.velocity.y = 0; |
| 160 | this.renderer.setAnimation("walkLeft"); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | else if (this.renderer.currentAnimationName && this.renderer.currentAnimationName.indexOf("walk") >= 0) |
| 165 | { |
| 166 | this.renderer.setAnimation(this.renderer.currentAnimationName.replace("walk", "stand")); |
| 167 | |
| 168 | this.transform.velocity.x = 0; |
| 169 | this.transform.velocity.y = 0; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | draw (context) |
| 174 | { |
| 175 | super.draw(context); |
| 176 | } |
| 177 | } |