BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

emotes
timfa Tim · pushed by timfa · 4 years ago
ce78c635 History
chao.js 5.0 KiB Download Raw
1class 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 this.renderer.sprite = this.scene.game.chaoImages.random();
63
64 this.renderer.defaultAnimationSpeed = 0.2;
65 this.registerBehaviour(new BehaviourEmote());
66 this.registerBehaviour(new BehaviourWander());
67
68 }
69
70 onClick (btn)
71 {
72 console.log(btn);
73 }
74
75 anyHeld ()
76 {
77 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
78 }
79
80 walkToPosition (position)
81 {
82 console.log("Walking towards", position);
83 this.targetDestination = position.getCopy();
84 this.prevPosition = this.transform.position.getCopy();
85 this.hBeforeV = Math.random() > 0.5;
86 }
87
88 update (deltaTime)
89 {
90 super.update(deltaTime);
91 if (GameInput.isPressed(GameInput.keys.a))
92 {
93 this.walkToPosition(GameInput.mousePosition.getCopy());
94 }
95
96 if (this.busyWithBehaviour)
97 this.currentBehaviour.update(deltaTime);
98 else
99 {
100 if (!this.walking)
101 {
102 this.behaviourTimer -= deltaTime;
103
104 if (this.behaviourTimer <= 0)
105 {
106 this.currentBehaviour = this.behaviourList.random();
107 console.log("Starting Behaviour '" + this.currentBehaviour.constructor.name + "'");
108 this.currentBehaviour.startBehaviour();
109 }
110 }
111 }
112
113 if (this.walking)
114 {
115 let halfTotalDistanceH = Math.abs(this.prevPosition.x - this.targetDestination.x) / 2;
116 let halfTotalDistanceV = Math.abs(this.prevPosition.y - this.targetDestination.y) / 2;
117
118 let currentDistanceH = Math.abs(this.transform.position.x - this.targetDestination.x);
119 let currentDistanceV = Math.abs(this.transform.position.y - this.targetDestination.y);
120
121 let hDir = -Math.sign(this.transform.position.x - this.targetDestination.x);
122 let vDir = -Math.sign(this.transform.position.y - this.targetDestination.y);
123
124 if (this.hBeforeV)
125 {
126 if (currentDistanceH > halfTotalDistanceH || currentDistanceV < 1)
127 {
128 this.renderer.mirrorX = hDir > 0;
129 this.transform.velocity.x = this.speed * hDir;
130 this.transform.velocity.y = 0;
131 this.renderer.setAnimation("walkLeft");
132 }
133 else
134 {
135 this.transform.velocity.x = 0;
136 this.transform.velocity.y = this.speed * vDir;
137 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
138 }
139 }
140 else
141 {
142 if (currentDistanceV > halfTotalDistanceV || currentDistanceH < 1)
143 {
144 this.transform.velocity.x = 0;
145 this.transform.velocity.y = this.speed * vDir;
146 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
147 }
148 else
149 {
150 this.renderer.mirrorX = hDir > 0;
151 this.transform.velocity.x = this.speed * hDir;
152 this.transform.velocity.y = 0;
153 this.renderer.setAnimation("walkLeft");
154 }
155 }
156 }
157 else if (this.renderer.currentAnimationName && this.renderer.currentAnimationName.indexOf("walk") >= 0)
158 {
159 this.renderer.setAnimation(this.renderer.currentAnimationName.replace("walk", "stand"));
160
161 this.transform.velocity.x = 0;
162 this.transform.velocity.y = 0;
163 }
164 }
165
166 draw (context)
167 {
168 super.draw(context);
169 }
170}