game.html 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. Title: Three.js Making a Game
  2. Description: Making a game with THREE.js
  3. Category: solutions
  4. TOC: Start making a Game.
  5. Many people want to write games using three.js. This article
  6. will hopefully give you some ideas on how to start.
  7. At least at the time I'm writing this article it's probably going to be the
  8. longest article on this site. It's possible the code here is massively over
  9. engineered but as I wrote each new feature I'd run into a problem that needed a
  10. solution I'm used to from other games I've written. In other words each new
  11. solution seemed important so I'll try to show why. Of course the smaller your
  12. game the less you might need some of the solutions shown here but this is a
  13. pretty small game and yet with the complexities of 3D characters many things
  14. take more organization than they might with 2D characters.
  15. As an example if you're making PacMan in 2D, when PacMan turns a corner
  16. that happens instantly at 90 degrees. There is no in-between step. But
  17. in a 3D game often we need the character to rotate over several frames.
  18. That simple change can add a bunch of complexity and require different
  19. solutions.
  20. The majority of the code here will not really be three.js and
  21. that's important to note, **three.js is not a game engine**.
  22. Three.js is a 3D library. It provides a [scene graph](threejs-scenegraph.html)
  23. and features for displaying 3D objects added to that scene graph
  24. but it does not provide all the other things needed to make a game.
  25. No collisions, no physics, no input systems, no path finding, etc, etc...
  26. So, we'll have to provide those things ourselves.
  27. I ended up writing quite a bit of code to make this simple *unfinished*
  28. game like thing and again, it's certainly possible I over engineered and there
  29. are simpler solutions but I feel like I actually didn't write
  30. enough code and hopefully I can explain what I think is missing.
  31. Many of the ideas here are heavily influenced by [Unity](https://unity.com).
  32. If you're not familiar with Unity that probably does not matter.
  33. I only bring it up as 10s of 1000s of games have shipped using
  34. these ideas.
  35. Let's start with the three.js parts. We need to load models for our game.
  36. At [opengameart.org](https://opengameart.org) I found this [animated knight
  37. model](https://opengameart.org/content/lowpoly-animated-knight) by [quaternius](https://opengameart.org/users/quaternius)
  38. <div class="threejs_center"><img src="resources/images/knight.jpg" style="width: 375px;"></div>
  39. [quaternius](https://opengameart.org/users/quaternius) also made [these animated animals](https://opengameart.org/content/lowpoly-animated-farm-animal-pack).
  40. <div class="threejs_center"><img src="resources/images/animals.jpg" style="width: 606px;"></div>
  41. These seem like good models to start with so the first thing we need to
  42. do is load them.
  43. We covered [loading glTF files before](threejs-load-gltf.html).
  44. The difference this time is we need to load multiple models and
  45. we can't start the game until all the models are loaded.
  46. Fortunately three.js provides the `LoadingManager` just for this purpose.
  47. We create a `LoadingManager` and pass it to the other loaders. The
  48. `LoadingManager` provides both [`onProgress`](LoadingManager.onProgress) and
  49. [`onLoad`](LoadingManager.onLoad) properties we can attach callbacks to.
  50. The [`onLoad`](LoadingManager.onLoad) callback will be called when
  51. all files have been loaded. The [`onProgress`](LoadingManager.onProgress) callback
  52. as called after each individual file arrives to give as a chance to show
  53. loading progress.
  54. Starting with the code from [loading a glTF file](threejs-load-gltf.html) I removed all
  55. the code related to framing the scene and added this code to load all models.
  56. ```js
  57. const manager = new THREE.LoadingManager();
  58. manager.onLoad = init;
  59. const models = {
  60. pig: { url: 'resources/models/animals/Pig.gltf' },
  61. cow: { url: 'resources/models/animals/Cow.gltf' },
  62. llama: { url: 'resources/models/animals/Llama.gltf' },
  63. pug: { url: 'resources/models/animals/Pug.gltf' },
  64. sheep: { url: 'resources/models/animals/Sheep.gltf' },
  65. zebra: { url: 'resources/models/animals/Zebra.gltf' },
  66. horse: { url: 'resources/models/animals/Horse.gltf' },
  67. knight: { url: 'resources/models/knight/KnightCharacter.gltf' },
  68. };
  69. {
  70. const gltfLoader = new GLTFLoader(manager);
  71. for (const model of Object.values(models)) {
  72. gltfLoader.load(model.url, (gltf) => {
  73. model.gltf = gltf;
  74. });
  75. }
  76. }
  77. function init() {
  78. // TBD
  79. }
  80. ```
  81. This code will load all the models above and the `LoadingManager` will call
  82. `init` when done. We'll use the `models` object later to let us access the
  83. loaded models so the `GLTFLoader` callback for each individual model attaches
  84. the loaded data to that model's info.
  85. All the models with all their animation are currently about 6.6meg. That's a
  86. pretty big download. Assuming your server supports compression (the server this
  87. site runs on does) it's able to compress them to around 1.4meg. That's
  88. definitely better than 6.6meg bit it's still not a tiny amount of data. It would
  89. probably be good if we added a progress bar so the user has some idea how much
  90. longer they have to wait.
  91. So, let's add an [`onProgress`](LoadingManager.onProgress) callback. It will be
  92. called with 3 arguments, the `url` of the last loaded object and then the number
  93. of items loaded so far as well as the total number of items.
  94. Let's setup some HTML for a loading bar
  95. ```html
  96. <body>
  97. <canvas id="c"></canvas>
  98. + <div id="loading">
  99. + <div>
  100. + <div>...loading...</div>
  101. + <div class="progress"><div id="progressbar"></div></div>
  102. + </div>
  103. + </div>
  104. </body>
  105. ```
  106. We'll look up the `#progressbar` div and we can set the width from 0% to 100%
  107. to show our progress. All we need to do is set that in our callback.
  108. ```js
  109. const manager = new THREE.LoadingManager();
  110. manager.onLoad = init;
  111. +const progressbarElem = document.querySelector('#progressbar');
  112. +manager.onProgress = (url, itemsLoaded, itemsTotal) => {
  113. + progressbarElem.style.width = `${itemsLoaded / itemsTotal * 100 | 0}%`;
  114. +};
  115. ```
  116. We already setup `init` to be called when all the models are loaded so
  117. we can turn off the progress bar by hiding the `#loading` element.
  118. ```js
  119. function init() {
  120. + // hide the loading bar
  121. + const loadingElem = document.querySelector('#loading');
  122. + loadingElem.style.display = 'none';
  123. }
  124. ```
  125. Here's a bunch of CSS for styling the bar. The CSS makes the `#loading` `<div>`
  126. the full size of the page and centers its children. The CSS makes a `.progress`
  127. area to contain the progress bar. The CSS also gives the progress bar
  128. a CSS animation of diagonal stripes.
  129. ```css
  130. #loading {
  131. position: absolute;
  132. left: 0;
  133. top: 0;
  134. width: 100%;
  135. height: 100%;
  136. display: flex;
  137. align-items: center;
  138. justify-content: center;
  139. text-align: center;
  140. font-size: xx-large;
  141. font-family: sans-serif;
  142. }
  143. #loading>div>div {
  144. padding: 2px;
  145. }
  146. .progress {
  147. width: 50vw;
  148. border: 1px solid black;
  149. }
  150. #progressbar {
  151. width: 0;
  152. transition: width ease-out .5s;
  153. height: 1em;
  154. background-color: #888;
  155. background-image: linear-gradient(
  156. -45deg,
  157. rgba(255, 255, 255, .5) 25%,
  158. transparent 25%,
  159. transparent 50%,
  160. rgba(255, 255, 255, .5) 50%,
  161. rgba(255, 255, 255, .5) 75%,
  162. transparent 75%,
  163. transparent
  164. );
  165. background-size: 50px 50px;
  166. animation: progressanim 2s linear infinite;
  167. }
  168. @keyframes progressanim {
  169. 0% {
  170. background-position: 50px 50px;
  171. }
  172. 100% {
  173. background-position: 0 0;
  174. }
  175. }
  176. ```
  177. Now that we have a progress bar let's deal with the models. These models
  178. have animations and we want to be able to access those animations.
  179. Animations are stored in an array by default be we'd like to be able to
  180. easily access them by name so let's setup an `animations` property for
  181. each model to do that. Note of course this means animations must have unique names.
  182. ```js
  183. +function prepModelsAndAnimations() {
  184. + Object.values(models).forEach(model => {
  185. + const animsByName = {};
  186. + model.gltf.animations.forEach((clip) => {
  187. + animsByName[clip.name] = clip;
  188. + });
  189. + model.animations = animsByName;
  190. + });
  191. +}
  192. function init() {
  193. // hide the loading bar
  194. const loadingElem = document.querySelector('#loading');
  195. loadingElem.style.display = 'none';
  196. + prepModelsAndAnimations();
  197. }
  198. ```
  199. Let's display the animated models.
  200. Unlike the [previous example of loading a glTF file](threejs-load-gltf.html)
  201. This time we probably want to be able to display more than one instance
  202. of each model. To do this, instead of adding
  203. the loaded gltf scene directly like we did in [the article on loading a glTF](threejs-load-gltf.html),
  204. we instead want to clone the scene and in particular we want to clone
  205. it for skinned animated characters. Fortunately there's a utility function,
  206. `SkeletonUtils.clone` we can use to do this. So, first we need to include
  207. the utils.
  208. ```js
  209. import * as THREE from './resources/three/r132/build/three.module.js';
  210. import {OrbitControls} from './resources/threejs/r132/examples/jsm/controls/OrbitControls.js';
  211. import {GLTFLoader} from './resources/threejs/r132/examples/jsm/loaders/GLTFLoader.js';
  212. +import * as SkeletonUtils from './resources/threejs/r132/examples/jsm/utils/SkeletonUtils.js';
  213. ```
  214. Then we can clone the models we just loaded
  215. ```js
  216. function init() {
  217. // hide the loading bar
  218. const loadingElem = document.querySelector('#loading');
  219. loadingElem.style.display = 'none';
  220. prepModelsAndAnimations();
  221. + Object.values(models).forEach((model, ndx) => {
  222. + const clonedScene = SkeletonUtils.clone(model.gltf.scene);
  223. + const root = new THREE.Object3D();
  224. + root.add(clonedScene);
  225. + scene.add(root);
  226. + root.position.x = (ndx - 3) * 3;
  227. + });
  228. }
  229. ```
  230. Above, for each model, we clone the `gltf.scene` we loaded and we parent that
  231. to a new `Object3D`. We need to parent it to another object because when
  232. we play animations the animation will apply animated positions to the nodes
  233. in the loaded scene which means we won't have control over those positions.
  234. To play the animations each model we clone needs an `AnimationMixer`.
  235. An `AnimationMixer` contains 1 or more `AnimationAction`s. An
  236. `AnimationAction` references an `AnimationClip`. `AnimationAction`s
  237. have all kinds of settings for playing then chaining to another
  238. action or cross fading between actions. Let's just get the first
  239. `AnimationClip` and create an action for it. The default is for
  240. an action to play its clip in a loop forever.
  241. ```js
  242. +const mixers = [];
  243. function init() {
  244. // hide the loading bar
  245. const loadingElem = document.querySelector('#loading');
  246. loadingElem.style.display = 'none';
  247. prepModelsAndAnimations();
  248. Object.values(models).forEach((model, ndx) => {
  249. const clonedScene = SkeletonUtils.clone(model.gltf.scene);
  250. const root = new THREE.Object3D();
  251. root.add(clonedScene);
  252. scene.add(root);
  253. root.position.x = (ndx - 3) * 3;
  254. + const mixer = new THREE.AnimationMixer(clonedScene);
  255. + const firstClip = Object.values(model.animations)[0];
  256. + const action = mixer.clipAction(firstClip);
  257. + action.play();
  258. + mixers.push(mixer);
  259. });
  260. }
  261. ```
  262. We called [`play`](AnimationAction.play) to start the action and stored
  263. off all the `AnimationMixers` in an array called `mixers`. Finally
  264. we need to update each `AnimationMixer` in our render loop by computing
  265. the time since the last frame and passing that to `AnimationMixer.update`.
  266. ```js
  267. +let then = 0;
  268. function render(now) {
  269. + now *= 0.001; // convert to seconds
  270. + const deltaTime = now - then;
  271. + then = now;
  272. if (resizeRendererToDisplaySize(renderer)) {
  273. const canvas = renderer.domElement;
  274. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  275. camera.updateProjectionMatrix();
  276. }
  277. + for (const mixer of mixers) {
  278. + mixer.update(deltaTime);
  279. + }
  280. renderer.render(scene, camera);
  281. requestAnimationFrame(render);
  282. }
  283. ```
  284. And with that we should get each model loaded and playing its first animation.
  285. {{{example url="../threejs-game-load-models.html"}}}
  286. Let's make it so we can check all of the animations.
  287. We'll add all of the clips as actions and then enable just one at
  288. a time.
  289. ```js
  290. -const mixers = [];
  291. +const mixerInfos = [];
  292. function init() {
  293. // hide the loading bar
  294. const loadingElem = document.querySelector('#loading');
  295. loadingElem.style.display = 'none';
  296. prepModelsAndAnimations();
  297. Object.values(models).forEach((model, ndx) => {
  298. const clonedScene = SkeletonUtils.clone(model.gltf.scene);
  299. const root = new THREE.Object3D();
  300. root.add(clonedScene);
  301. scene.add(root);
  302. root.position.x = (ndx - 3) * 3;
  303. const mixer = new THREE.AnimationMixer(clonedScene);
  304. - const firstClip = Object.values(model.animations)[0];
  305. - const action = mixer.clipAction(firstClip);
  306. - action.play();
  307. - mixers.push(mixer);
  308. + const actions = Object.values(model.animations).map((clip) => {
  309. + return mixer.clipAction(clip);
  310. + });
  311. + const mixerInfo = {
  312. + mixer,
  313. + actions,
  314. + actionNdx: -1,
  315. + };
  316. + mixerInfos.push(mixerInfo);
  317. + playNextAction(mixerInfo);
  318. });
  319. }
  320. +function playNextAction(mixerInfo) {
  321. + const {actions, actionNdx} = mixerInfo;
  322. + const nextActionNdx = (actionNdx + 1) % actions.length;
  323. + mixerInfo.actionNdx = nextActionNdx;
  324. + actions.forEach((action, ndx) => {
  325. + const enabled = ndx === nextActionNdx;
  326. + action.enabled = enabled;
  327. + if (enabled) {
  328. + action.play();
  329. + }
  330. + });
  331. +}
  332. ```
  333. The code above makes an array of `AnimationAction`s,
  334. one for each `AnimationClip`. It makes an array of objects, `mixerInfos`,
  335. with references to the `AnimationMixer` and all the `AnimationAction`s
  336. for each model. It then calls `playNextAction` which sets `enabled` on
  337. all but one action for that mixer.
  338. We need to update the render loop for the new array
  339. ```js
  340. -for (const mixer of mixers) {
  341. +for (const {mixer} of mixerInfos) {
  342. mixer.update(deltaTime);
  343. }
  344. ```
  345. Let's make it so pressing a key 1 to 8 will play the next animation
  346. for each model
  347. ```js
  348. window.addEventListener('keydown', (e) => {
  349. const mixerInfo = mixerInfos[e.keyCode - 49];
  350. if (!mixerInfo) {
  351. return;
  352. }
  353. playNextAction(mixerInfo);
  354. });
  355. ```
  356. Now you should be able to click on the example and then press keys 1 through 8
  357. to cycle each of the models through their available animations.
  358. {{{example url="../threejs-game-check-animations.html"}}}
  359. So that is arguably the sum-total of the three.js portion of this
  360. article. We covered loading multiple files, cloning skinned models,
  361. and playing animations on them. In a real game you'd have to do a
  362. ton more manipulation of `AnimationAction` objects.
  363. Let's start making a game infrastructure
  364. A common pattern for making a modern game is to use an
  365. [Entity Component System](https://www.google.com/search?q=entity+component+system).
  366. In an Entity Component System an object in a game is called an *entity* that consists
  367. of a bunch of *components*. You build up entities by deciding which components to
  368. attach to them. So, let's make an Entity Component System.
  369. We'll call our entities `GameObject`. It's effectively just a collection
  370. of components and a three.js `Object3D`.
  371. ```js
  372. function removeArrayElement(array, element) {
  373. const ndx = array.indexOf(element);
  374. if (ndx >= 0) {
  375. array.splice(ndx, 1);
  376. }
  377. }
  378. class GameObject {
  379. constructor(parent, name) {
  380. this.name = name;
  381. this.components = [];
  382. this.transform = new THREE.Object3D();
  383. parent.add(this.transform);
  384. }
  385. addComponent(ComponentType, ...args) {
  386. const component = new ComponentType(this, ...args);
  387. this.components.push(component);
  388. return component;
  389. }
  390. removeComponent(component) {
  391. removeArrayElement(this.components, component);
  392. }
  393. getComponent(ComponentType) {
  394. return this.components.find(c => c instanceof ComponentType);
  395. }
  396. update() {
  397. for (const component of this.components) {
  398. component.update();
  399. }
  400. }
  401. }
  402. ```
  403. Calling `GameObject.update` calls `update` on all the components.
  404. I included a name only to help in debugging so if I look at a `GameObject`
  405. in the debugger I can see a name to help identify it.
  406. Some things that might seem a little strange:
  407. `GameObject.addComponent` is used to create components. Whether or not
  408. this a good idea or a bad idea I'm not sure. My thinking was it makes
  409. no sense for a component to exist outside of a gameobject so I thought
  410. it might be good if creating a component automatically added that component
  411. to the gameobject and passed the gameobject to the component's constructor.
  412. In other words to add a component you do this
  413. ```js
  414. const gameObject = new GameObject(scene, 'foo');
  415. gameObject.addComponent(TypeOfComponent);
  416. ```
  417. If I didn't do it this way you'd instead do something like this
  418. ```js
  419. const gameObject = new GameObject(scene, 'foo');
  420. const component = new TypeOfComponent(gameObject);
  421. gameObject.addComponent(component);
  422. ```
  423. Is it better that the first way is shorter and more automated or is it worse
  424. because it looks out of the ordinary? I don't know.
  425. `GameObject.getComponent` looks up components by type. That has
  426. the implication that you can not have 2 components of the same
  427. type on a single game object or at least if you do you can only
  428. look up the first one without adding some other API.
  429. It's common for one component to look up another and when looking them up they
  430. have to match by type otherwise you might get the wrong one. We could instead
  431. give each component a name and you could look them up by name. That would be
  432. more flexible in that you could have more than one component of the same type but it
  433. would also be more tedious. Again, I'm not sure which is better.
  434. On to the components themselves. Here is their base class.
  435. ```js
  436. // Base for all components
  437. class Component {
  438. constructor(gameObject) {
  439. this.gameObject = gameObject;
  440. }
  441. update() {
  442. }
  443. }
  444. ```
  445. Do components need a base class? JavaScript is not like most strictly
  446. typed languages so effectively we could have no base class and just
  447. leave it up to each component to do whatever it wants in its constructor
  448. knowing that the first argument is always the component's gameobject.
  449. If it doesn't care about gameobject it wouldn't store it. I kind of feel like this
  450. common base is good though. It means if you have a reference to a
  451. component you know you can find its parent gameobject always and from its
  452. parent you can easily look up other components as well as look at its
  453. transform.
  454. To manage the gameobjects we probably need some kind of gameobject manager. You
  455. might think we could just keep an array of gameobjects but in a real game the
  456. components of a gameobject might add and remove other gameobjects at runtime.
  457. For example a gun gameobject might add a bullet gameobject every time the gun
  458. fires. A monster gameobject might remove itself if it has been killed. We then
  459. would have an issue that we might have code like this
  460. ```js
  461. for (const gameObject of globalArrayOfGameObjects) {
  462. gameObject.update();
  463. }
  464. ```
  465. The loop above would fail or do un-expected things if
  466. gameobjects are added or removed from `globalArrayOfGameObjects`
  467. in the middle of the loop in some component's `update` function.
  468. To try to prevent that problem we need something a little safer.
  469. Here's one attempt.
  470. ```js
  471. class SafeArray {
  472. constructor() {
  473. this.array = [];
  474. this.addQueue = [];
  475. this.removeQueue = new Set();
  476. }
  477. get isEmpty() {
  478. return this.addQueue.length + this.array.length > 0;
  479. }
  480. add(element) {
  481. this.addQueue.push(element);
  482. }
  483. remove(element) {
  484. this.removeQueue.add(element);
  485. }
  486. forEach(fn) {
  487. this._addQueued();
  488. this._removeQueued();
  489. for (const element of this.array) {
  490. if (this.removeQueue.has(element)) {
  491. continue;
  492. }
  493. fn(element);
  494. }
  495. this._removeQueued();
  496. }
  497. _addQueued() {
  498. if (this.addQueue.length) {
  499. this.array.splice(this.array.length, 0, ...this.addQueue);
  500. this.addQueue = [];
  501. }
  502. }
  503. _removeQueued() {
  504. if (this.removeQueue.size) {
  505. this.array = this.array.filter(element => !this.removeQueue.has(element));
  506. this.removeQueue.clear();
  507. }
  508. }
  509. }
  510. ```
  511. The class above lets you add or remove elements from the `SafeArray`
  512. but won't mess with the array itself while it's being iterated over. Instead
  513. new elements get added to `addQueue` and removed elements to the `removeQueue`
  514. and then added or removed outside of the loop.
  515. Using that here is our class to manage gameobjects.
  516. ```js
  517. class GameObjectManager {
  518. constructor() {
  519. this.gameObjects = new SafeArray();
  520. }
  521. createGameObject(parent, name) {
  522. const gameObject = new GameObject(parent, name);
  523. this.gameObjects.add(gameObject);
  524. return gameObject;
  525. }
  526. removeGameObject(gameObject) {
  527. this.gameObjects.remove(gameObject);
  528. }
  529. update() {
  530. this.gameObjects.forEach(gameObject => gameObject.update());
  531. }
  532. }
  533. ```
  534. With all that now let's make our first component. This component
  535. will just manage a skinned three.js object like the ones we just created.
  536. To keep it simple it will just have one method, `setAnimation` that
  537. takes the name of the animation to play and plays it.
  538. ```js
  539. class SkinInstance extends Component {
  540. constructor(gameObject, model) {
  541. super(gameObject);
  542. this.model = model;
  543. this.animRoot = SkeletonUtils.clone(this.model.gltf.scene);
  544. this.mixer = new THREE.AnimationMixer(this.animRoot);
  545. gameObject.transform.add(this.animRoot);
  546. this.actions = {};
  547. }
  548. setAnimation(animName) {
  549. const clip = this.model.animations[animName];
  550. // turn off all current actions
  551. for (const action of Object.values(this.actions)) {
  552. action.enabled = false;
  553. }
  554. // get or create existing action for clip
  555. const action = this.mixer.clipAction(clip);
  556. action.enabled = true;
  557. action.reset();
  558. action.play();
  559. this.actions[animName] = action;
  560. }
  561. update() {
  562. this.mixer.update(globals.deltaTime);
  563. }
  564. }
  565. ```
  566. You can see it's basically the code we had before that clones the scene we loaded,
  567. then sets up an `AnimationMixer`. `setAnimation` adds a `AnimationAction` for a
  568. particular `AnimationClip` if one does not already exist and disables all
  569. existing actions.
  570. The code references `globals.deltaTime`. Let's make a globals object
  571. ```js
  572. const globals = {
  573. time: 0,
  574. deltaTime: 0,
  575. };
  576. ```
  577. And update it in the render loop
  578. ```js
  579. let then = 0;
  580. function render(now) {
  581. // convert to seconds
  582. globals.time = now * 0.001;
  583. // make sure delta time isn't too big.
  584. globals.deltaTime = Math.min(globals.time - then, 1 / 20);
  585. then = globals.time;
  586. ```
  587. The check above for making sure `deltaTime` is not more than 1/20th
  588. of a second is because otherwise we'd get a huge value for `deltaTime`
  589. if we hide the tab. We might hide it for seconds or minutes and then
  590. when our tab was brought to the front `deltaTime` would be huge
  591. and might teleport characters across our game world if we had code like
  592. ```js
  593. position += velocity * deltaTime;
  594. ```
  595. By limiting the maximum `deltaTime` that issue is prevented.
  596. Now let's make a component for the player.
  597. ```js
  598. class Player extends Component {
  599. constructor(gameObject) {
  600. super(gameObject);
  601. const model = models.knight;
  602. this.skinInstance = gameObject.addComponent(SkinInstance, model);
  603. this.skinInstance.setAnimation('Run');
  604. }
  605. }
  606. ```
  607. The player calls `setAnimation` with `'Run'`. To know which animations
  608. are available I modified our previous example to print out the names of
  609. the animations
  610. ```js
  611. function prepModelsAndAnimations() {
  612. Object.values(models).forEach(model => {
  613. + console.log('------->:', model.url);
  614. const animsByName = {};
  615. model.gltf.animations.forEach((clip) => {
  616. animsByName[clip.name] = clip;
  617. + console.log(' ', clip.name);
  618. });
  619. model.animations = animsByName;
  620. });
  621. }
  622. ```
  623. And running it got this list in [the JavaScript console](https://developers.google.com/web/tools/chrome-devtools/console/javascript).
  624. ```
  625. ------->: resources/models/animals/Pig.gltf
  626. Idle
  627. Death
  628. WalkSlow
  629. Jump
  630. Walk
  631. ------->: resources/models/animals/Cow.gltf
  632. Walk
  633. Jump
  634. WalkSlow
  635. Death
  636. Idle
  637. ------->: resources/models/animals/Llama.gltf
  638. Jump
  639. Idle
  640. Walk
  641. Death
  642. WalkSlow
  643. ------->: resources/models/animals/Pug.gltf
  644. Jump
  645. Walk
  646. Idle
  647. WalkSlow
  648. Death
  649. ------->: resources/models/animals/Sheep.gltf
  650. WalkSlow
  651. Death
  652. Jump
  653. Walk
  654. Idle
  655. ------->: resources/models/animals/Zebra.gltf
  656. Jump
  657. Walk
  658. Death
  659. WalkSlow
  660. Idle
  661. ------->: resources/models/animals/Horse.gltf
  662. Jump
  663. WalkSlow
  664. Death
  665. Walk
  666. Idle
  667. ------->: resources/models/knight/KnightCharacter.gltf
  668. Run_swordRight
  669. Run
  670. Idle_swordLeft
  671. Roll_sword
  672. Idle
  673. Run_swordAttack
  674. ```
  675. Fortunately the names of the animations for all the animals match
  676. which will come in handy later. For now we only care the that the
  677. player has an animation called `Run`.
  678. Let's use these components. Here's the updated init function.
  679. All it does is create a `GameObject` and add a `Player` component to it.
  680. ```js
  681. const globals = {
  682. time: 0,
  683. deltaTime: 0,
  684. };
  685. +const gameObjectManager = new GameObjectManager();
  686. function init() {
  687. // hide the loading bar
  688. const loadingElem = document.querySelector('#loading');
  689. loadingElem.style.display = 'none';
  690. prepModelsAndAnimations();
  691. + {
  692. + const gameObject = gameObjectManager.createGameObject(scene, 'player');
  693. + gameObject.addComponent(Player);
  694. + }
  695. }
  696. ```
  697. And we need to call `gameObjectManager.update` in our render loop
  698. ```js
  699. let then = 0;
  700. function render(now) {
  701. // convert to seconds
  702. globals.time = now * 0.001;
  703. // make sure delta time isn't too big.
  704. globals.deltaTime = Math.min(globals.time - then, 1 / 20);
  705. then = globals.time;
  706. if (resizeRendererToDisplaySize(renderer)) {
  707. const canvas = renderer.domElement;
  708. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  709. camera.updateProjectionMatrix();
  710. }
  711. - for (const {mixer} of mixerInfos) {
  712. - mixer.update(deltaTime);
  713. - }
  714. + gameObjectManager.update();
  715. renderer.render(scene, camera);
  716. requestAnimationFrame(render);
  717. }
  718. ```
  719. and if we run that we get a single player.
  720. {{{example url="../threejs-game-just-player.html"}}}
  721. That was a lot of code just for an entity component system but
  722. it's infrastructure that most games need.
  723. Let's add an input system. Rather than read keys directly we'll
  724. make a class that other parts of the code can check `left` or `right`.
  725. That way we can assign multiple ways to input `left` or `right` etc..
  726. We'll start with just keys
  727. ```js
  728. // Keeps the state of keys/buttons
  729. //
  730. // You can check
  731. //
  732. // inputManager.keys.left.down
  733. //
  734. // to see if the left key is currently held down
  735. // and you can check
  736. //
  737. // inputManager.keys.left.justPressed
  738. //
  739. // To see if the left key was pressed this frame
  740. //
  741. // Keys are 'left', 'right', 'a', 'b', 'up', 'down'
  742. class InputManager {
  743. constructor() {
  744. this.keys = {};
  745. const keyMap = new Map();
  746. const setKey = (keyName, pressed) => {
  747. const keyState = this.keys[keyName];
  748. keyState.justPressed = pressed && !keyState.down;
  749. keyState.down = pressed;
  750. };
  751. const addKey = (keyCode, name) => {
  752. this.keys[name] = { down: false, justPressed: false };
  753. keyMap.set(keyCode, name);
  754. };
  755. const setKeyFromKeyCode = (keyCode, pressed) => {
  756. const keyName = keyMap.get(keyCode);
  757. if (!keyName) {
  758. return;
  759. }
  760. setKey(keyName, pressed);
  761. };
  762. addKey(37, 'left');
  763. addKey(39, 'right');
  764. addKey(38, 'up');
  765. addKey(40, 'down');
  766. addKey(90, 'a');
  767. addKey(88, 'b');
  768. window.addEventListener('keydown', (e) => {
  769. setKeyFromKeyCode(e.keyCode, true);
  770. });
  771. window.addEventListener('keyup', (e) => {
  772. setKeyFromKeyCode(e.keyCode, false);
  773. });
  774. }
  775. update() {
  776. for (const keyState of Object.values(this.keys)) {
  777. if (keyState.justPressed) {
  778. keyState.justPressed = false;
  779. }
  780. }
  781. }
  782. }
  783. ```
  784. The code above tracks whether keys are up or down and you can check
  785. if a key is currently pressed by checking for example
  786. `inputManager.keys.left.down`. It also has a `justPressed` property
  787. for each key so that you can check the user just pressed the key.
  788. For example a jump key you don't want to know if the button is being
  789. held down, you want to know did the user press it now.
  790. Let's create an instance of `InputManager`
  791. ```js
  792. const globals = {
  793. time: 0,
  794. deltaTime: 0,
  795. };
  796. const gameObjectManager = new GameObjectManager();
  797. +const inputManager = new InputManager();
  798. ```
  799. and update it in our render loop
  800. ```js
  801. function render(now) {
  802. ...
  803. gameObjectManager.update();
  804. + inputManager.update();
  805. ...
  806. }
  807. ```
  808. It needs to be called after `gameObjectManager.update` otherwise
  809. `justPressed` would never be true inside a component's `update` function.
  810. Let's use it in the `Player` component
  811. ```js
  812. +const kForward = new THREE.Vector3(0, 0, 1);
  813. const globals = {
  814. time: 0,
  815. deltaTime: 0,
  816. + moveSpeed: 16,
  817. };
  818. class Player extends Component {
  819. constructor(gameObject) {
  820. super(gameObject);
  821. const model = models.knight;
  822. this.skinInstance = gameObject.addComponent(SkinInstance, model);
  823. this.skinInstance.setAnimation('Run');
  824. + this.turnSpeed = globals.moveSpeed / 4;
  825. }
  826. + update() {
  827. + const {deltaTime, moveSpeed} = globals;
  828. + const {transform} = this.gameObject;
  829. + const delta = (inputManager.keys.left.down ? 1 : 0) +
  830. + (inputManager.keys.right.down ? -1 : 0);
  831. + transform.rotation.y += this.turnSpeed * delta * deltaTime;
  832. + transform.translateOnAxis(kForward, moveSpeed * deltaTime);
  833. + }
  834. }
  835. ```
  836. The code above uses `Object3D.transformOnAxis` to move the player
  837. forward. `Object3D.transformOnAxis` works in local space so it only
  838. works if the object in question is at the root of the scene, not if it's
  839. parented to something else <a class="footnote" href="#parented" id="parented-backref">1</a>
  840. We also added a global `moveSpeed` and based a `turnSpeed` on the move speed.
  841. The turn speed is based on the move speed to try to make sure a character
  842. can turn sharply enough to meet its target. If `turnSpeed` so too small
  843. a character will turn around and around circling its target but never
  844. hitting it. I didn't bother to do the math to calculate the required
  845. turn speed for a given move speed. I just guessed.
  846. The code so far would work but if the player runs off the screen there's no
  847. way to find out where they are. Let's make it so if they are offscreen
  848. for more than a certain time they get teleported back to the origin.
  849. We can do that by using the three.js `Frustum` class to check if a point
  850. is inside the camera's view frustum.
  851. We need to build a frustum from the camera. We could do this in the Player
  852. component but other objects might want to use this too so let's add another
  853. gameobject with a component to manage a frustum.
  854. ```js
  855. class CameraInfo extends Component {
  856. constructor(gameObject) {
  857. super(gameObject);
  858. this.projScreenMatrix = new THREE.Matrix4();
  859. this.frustum = new THREE.Frustum();
  860. }
  861. update() {
  862. const {camera} = globals;
  863. this.projScreenMatrix.multiplyMatrices(
  864. camera.projectionMatrix,
  865. camera.matrixWorldInverse);
  866. this.frustum.setFromProjectionMatrix(this.projScreenMatrix);
  867. }
  868. }
  869. ```
  870. Then let's setup another gameobject at init time.
  871. ```js
  872. function init() {
  873. // hide the loading bar
  874. const loadingElem = document.querySelector('#loading');
  875. loadingElem.style.display = 'none';
  876. prepModelsAndAnimations();
  877. + {
  878. + const gameObject = gameObjectManager.createGameObject(camera, 'camera');
  879. + globals.cameraInfo = gameObject.addComponent(CameraInfo);
  880. + }
  881. {
  882. const gameObject = gameObjectManager.createGameObject(scene, 'player');
  883. gameObject.addComponent(Player);
  884. }
  885. }
  886. ```
  887. and now we can use it in the `Player` component.
  888. ```js
  889. class Player extends Component {
  890. constructor(gameObject) {
  891. super(gameObject);
  892. const model = models.knight;
  893. this.skinInstance = gameObject.addComponent(SkinInstance, model);
  894. this.skinInstance.setAnimation('Run');
  895. this.turnSpeed = globals.moveSpeed / 4;
  896. + this.offscreenTimer = 0;
  897. + this.maxTimeOffScreen = 3;
  898. }
  899. update() {
  900. - const {deltaTime, moveSpeed} = globals;
  901. + const {deltaTime, moveSpeed, cameraInfo} = globals;
  902. const {transform} = this.gameObject;
  903. const delta = (inputManager.keys.left.down ? 1 : 0) +
  904. (inputManager.keys.right.down ? -1 : 0);
  905. transform.rotation.y += this.turnSpeed * delta * deltaTime;
  906. transform.translateOnAxis(kForward, moveSpeed * deltaTime);
  907. + const {frustum} = cameraInfo;
  908. + if (frustum.containsPoint(transform.position)) {
  909. + this.offscreenTimer = 0;
  910. + } else {
  911. + this.offscreenTimer += deltaTime;
  912. + if (this.offscreenTimer >= this.maxTimeOffScreen) {
  913. + transform.position.set(0, 0, 0);
  914. + }
  915. + }
  916. }
  917. }
  918. ```
  919. One more thing before we try it out, let's add touchscreen support
  920. for mobile. First let's add some HTML to touch
  921. ```html
  922. <body>
  923. <canvas id="c"></canvas>
  924. + <div id="ui">
  925. + <div id="left"><img src="resources/images/left.svg"></div>
  926. + <div style="flex: 0 0 40px;"></div>
  927. + <div id="right"><img src="resources/images/right.svg"></div>
  928. + </div>
  929. <div id="loading">
  930. <div>
  931. <div>...loading...</div>
  932. <div class="progress"><div id="progressbar"></div></div>
  933. </div>
  934. </div>
  935. </body>
  936. ```
  937. and some CSS to style it
  938. ```css
  939. #ui {
  940. position: absolute;
  941. left: 0;
  942. top: 0;
  943. width: 100%;
  944. height: 100%;
  945. display: flex;
  946. justify-items: center;
  947. align-content: stretch;
  948. }
  949. #ui>div {
  950. display: flex;
  951. align-items: flex-end;
  952. flex: 1 1 auto;
  953. }
  954. .bright {
  955. filter: brightness(2);
  956. }
  957. #left {
  958. justify-content: flex-end;
  959. }
  960. #right {
  961. justify-content: flex-start;
  962. }
  963. #ui img {
  964. padding: 10px;
  965. width: 80px;
  966. height: 80px;
  967. display: block;
  968. }
  969. ```
  970. The idea here is there is one div, `#ui`, that
  971. covers the entire page. Inside will be 2 divs, `#left` and `#right`
  972. both of which are almost half the page wide and the entire screen tall.
  973. In between there is a 40px separator. If the user slides their finger
  974. over the left or right side then we need up update `keys.left` and `keys.right`
  975. in the `InputManager`. This makes the entire screen sensitive to being touched
  976. which seemed better than just small arrows.
  977. ```js
  978. class InputManager {
  979. constructor() {
  980. this.keys = {};
  981. const keyMap = new Map();
  982. const setKey = (keyName, pressed) => {
  983. const keyState = this.keys[keyName];
  984. keyState.justPressed = pressed && !keyState.down;
  985. keyState.down = pressed;
  986. };
  987. const addKey = (keyCode, name) => {
  988. this.keys[name] = { down: false, justPressed: false };
  989. keyMap.set(keyCode, name);
  990. };
  991. const setKeyFromKeyCode = (keyCode, pressed) => {
  992. const keyName = keyMap.get(keyCode);
  993. if (!keyName) {
  994. return;
  995. }
  996. setKey(keyName, pressed);
  997. };
  998. addKey(37, 'left');
  999. addKey(39, 'right');
  1000. addKey(38, 'up');
  1001. addKey(40, 'down');
  1002. addKey(90, 'a');
  1003. addKey(88, 'b');
  1004. window.addEventListener('keydown', (e) => {
  1005. setKeyFromKeyCode(e.keyCode, true);
  1006. });
  1007. window.addEventListener('keyup', (e) => {
  1008. setKeyFromKeyCode(e.keyCode, false);
  1009. });
  1010. + const sides = [
  1011. + { elem: document.querySelector('#left'), key: 'left' },
  1012. + { elem: document.querySelector('#right'), key: 'right' },
  1013. + ];
  1014. +
  1015. + const clearKeys = () => {
  1016. + for (const {key} of sides) {
  1017. + setKey(key, false);
  1018. + }
  1019. + };
  1020. +
  1021. + const handleMouseMove = (e) => {
  1022. + e.preventDefault();
  1023. + // this is needed because we call preventDefault();
  1024. + // we also gave the canvas a tabindex so it can
  1025. + // become the focus
  1026. + canvas.focus();
  1027. + window.addEventListener('pointermove', handleMouseMove);
  1028. + window.addEventListener('pointerup', handleMouseUp);
  1029. +
  1030. + for (const {elem, key} of sides) {
  1031. + let pressed = false;
  1032. + const rect = elem.getBoundingClientRect();
  1033. + const x = e.clientX;
  1034. + const y = e.clientY;
  1035. + const inRect = x >= rect.left && x < rect.right &&
  1036. + y >= rect.top && y < rect.bottom;
  1037. + if (inRect) {
  1038. + pressed = true;
  1039. + }
  1040. + setKey(key, pressed);
  1041. + }
  1042. + };
  1043. +
  1044. + function handleMouseUp() {
  1045. + clearKeys();
  1046. + window.removeEventListener('pointermove', handleMouseMove, {passive: false});
  1047. + window.removeEventListener('pointerup', handleMouseUp);
  1048. + }
  1049. +
  1050. + const uiElem = document.querySelector('#ui');
  1051. + uiElem.addEventListener('pointerdown', handleMouseMove, {passive: false});
  1052. +
  1053. + uiElem.addEventListener('touchstart', (e) => {
  1054. + // prevent scrolling
  1055. + e.preventDefault();
  1056. + }, {passive: false});
  1057. }
  1058. update() {
  1059. for (const keyState of Object.values(this.keys)) {
  1060. if (keyState.justPressed) {
  1061. keyState.justPressed = false;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. ```
  1067. And now we should be able to control the character with the left and right
  1068. cursor keys or with our fingers on a touchscreen
  1069. {{{example url="../threejs-game-player-input.html"}}}
  1070. Ideally we'd do something else if the player went off the screen like move
  1071. the camera or maybe offscreen = death but this article is already going to be
  1072. too long so for now teleporting to the middle was the simplest thing.
  1073. Lets add some animals. We can start it off similar to the `Player` by making
  1074. an `Animal` component.
  1075. ```js
  1076. class Animal extends Component {
  1077. constructor(gameObject, model) {
  1078. super(gameObject);
  1079. const skinInstance = gameObject.addComponent(SkinInstance, model);
  1080. skinInstance.mixer.timeScale = globals.moveSpeed / 4;
  1081. skinInstance.setAnimation('Idle');
  1082. }
  1083. }
  1084. ```
  1085. The code above sets the `AnimationMixer.timeScale` to set the playback
  1086. speed of the animations relative to the move speed. This way if we
  1087. adjust the move speed the animation will speed up or slow down as well.
  1088. To start we could setup one of each type of animal
  1089. ```js
  1090. function init() {
  1091. // hide the loading bar
  1092. const loadingElem = document.querySelector('#loading');
  1093. loadingElem.style.display = 'none';
  1094. prepModelsAndAnimations();
  1095. {
  1096. const gameObject = gameObjectManager.createGameObject(camera, 'camera');
  1097. globals.cameraInfo = gameObject.addComponent(CameraInfo);
  1098. }
  1099. {
  1100. const gameObject = gameObjectManager.createGameObject(scene, 'player');
  1101. globals.player = gameObject.addComponent(Player);
  1102. globals.congaLine = [gameObject];
  1103. }
  1104. + const animalModelNames = [
  1105. + 'pig',
  1106. + 'cow',
  1107. + 'llama',
  1108. + 'pug',
  1109. + 'sheep',
  1110. + 'zebra',
  1111. + 'horse',
  1112. + ];
  1113. + animalModelNames.forEach((name, ndx) => {
  1114. + const gameObject = gameObjectManager.createGameObject(scene, name);
  1115. + gameObject.addComponent(Animal, models[name]);
  1116. + gameObject.transform.position.x = (ndx + 1) * 5;
  1117. + });
  1118. }
  1119. ```
  1120. And that would get us animals standing on the screen but we want them to do
  1121. something.
  1122. Let's make them follow the player in a conga line but only if the player gets near enough.
  1123. To do this we need several states.
  1124. * Idle:
  1125. Animal is waiting for player to get close
  1126. * Wait for End of Line:
  1127. Animal was tagged by player but now needs to wait for the animal
  1128. at the end of the line to come by so they can join the end of the line.
  1129. * Go to Last:
  1130. Animal needs to walk to where the animal they are following was, at the same time recording
  1131. a history of where the animal they are following is currently.
  1132. * Follow
  1133. Animal needs to keep recording a history of where the animal they are following is while
  1134. moving to where the animal they are following was before.
  1135. There are many ways to handle different states like this. A common one is to use
  1136. a [Finite State Machine](https://www.google.com/search?q=finite+state+machine) and
  1137. to build some class to help us manage the state.
  1138. So, let's do that.
  1139. ```js
  1140. class FiniteStateMachine {
  1141. constructor(states, initialState) {
  1142. this.states = states;
  1143. this.transition(initialState);
  1144. }
  1145. get state() {
  1146. return this.currentState;
  1147. }
  1148. transition(state) {
  1149. const oldState = this.states[this.currentState];
  1150. if (oldState && oldState.exit) {
  1151. oldState.exit.call(this);
  1152. }
  1153. this.currentState = state;
  1154. const newState = this.states[state];
  1155. if (newState.enter) {
  1156. newState.enter.call(this);
  1157. }
  1158. }
  1159. update() {
  1160. const state = this.states[this.currentState];
  1161. if (state.update) {
  1162. state.update.call(this);
  1163. }
  1164. }
  1165. }
  1166. ```
  1167. Here's a simple class. We pass it an object with a bunch of states.
  1168. Each state as 3 optional functions, `enter`, `update`, and `exit`.
  1169. To switch states we call `FiniteStateMachine.transition` and pass it
  1170. the name of the new state. If the current state has an `exit` function
  1171. it's called. Then if the new state has an `enter` function it's called.
  1172. Finally each frame `FiniteStateMachine.update` calls the `update` function
  1173. of the current state.
  1174. Let's use it to manage the states of the animals.
  1175. ```js
  1176. // Returns true of obj1 and obj2 are close
  1177. function isClose(obj1, obj1Radius, obj2, obj2Radius) {
  1178. const minDist = obj1Radius + obj2Radius;
  1179. const dist = obj1.position.distanceTo(obj2.position);
  1180. return dist < minDist;
  1181. }
  1182. // keeps v between -min and +min
  1183. function minMagnitude(v, min) {
  1184. return Math.abs(v) > min
  1185. ? min * Math.sign(v)
  1186. : v;
  1187. }
  1188. const aimTowardAndGetDistance = function() {
  1189. const delta = new THREE.Vector3();
  1190. return function aimTowardAndGetDistance(source, targetPos, maxTurn) {
  1191. delta.subVectors(targetPos, source.position);
  1192. // compute the direction we want to be facing
  1193. const targetRot = Math.atan2(delta.x, delta.z) + Math.PI * 1.5;
  1194. // rotate in the shortest direction
  1195. const deltaRot = (targetRot - source.rotation.y + Math.PI * 1.5) % (Math.PI * 2) - Math.PI;
  1196. // make sure we don't turn faster than maxTurn
  1197. const deltaRotation = minMagnitude(deltaRot, maxTurn);
  1198. // keep rotation between 0 and Math.PI * 2
  1199. source.rotation.y = THREE.MathUtils.euclideanModulo(
  1200. source.rotation.y + deltaRotation, Math.PI * 2);
  1201. // return the distance to the target
  1202. return delta.length();
  1203. };
  1204. }();
  1205. class Animal extends Component {
  1206. constructor(gameObject, model) {
  1207. super(gameObject);
  1208. + const hitRadius = model.size / 2;
  1209. const skinInstance = gameObject.addComponent(SkinInstance, model);
  1210. skinInstance.mixer.timeScale = globals.moveSpeed / 4;
  1211. + const transform = gameObject.transform;
  1212. + const playerTransform = globals.player.gameObject.transform;
  1213. + const maxTurnSpeed = Math.PI * (globals.moveSpeed / 4);
  1214. + const targetHistory = [];
  1215. + let targetNdx = 0;
  1216. +
  1217. + function addHistory() {
  1218. + const targetGO = globals.congaLine[targetNdx];
  1219. + const newTargetPos = new THREE.Vector3();
  1220. + newTargetPos.copy(targetGO.transform.position);
  1221. + targetHistory.push(newTargetPos);
  1222. + }
  1223. +
  1224. + this.fsm = new FiniteStateMachine({
  1225. + idle: {
  1226. + enter: () => {
  1227. + skinInstance.setAnimation('Idle');
  1228. + },
  1229. + update: () => {
  1230. + // check if player is near
  1231. + if (isClose(transform, hitRadius, playerTransform, globals.playerRadius)) {
  1232. + this.fsm.transition('waitForEnd');
  1233. + }
  1234. + },
  1235. + },
  1236. + waitForEnd: {
  1237. + enter: () => {
  1238. + skinInstance.setAnimation('Jump');
  1239. + },
  1240. + update: () => {
  1241. + // get the gameObject at the end of the conga line
  1242. + const lastGO = globals.congaLine[globals.congaLine.length - 1];
  1243. + const deltaTurnSpeed = maxTurnSpeed * globals.deltaTime;
  1244. + const targetPos = lastGO.transform.position;
  1245. + aimTowardAndGetDistance(transform, targetPos, deltaTurnSpeed);
  1246. + // check if last thing in conga line is near
  1247. + if (isClose(transform, hitRadius, lastGO.transform, globals.playerRadius)) {
  1248. + this.fsm.transition('goToLast');
  1249. + }
  1250. + },
  1251. + },
  1252. + goToLast: {
  1253. + enter: () => {
  1254. + // remember who we're following
  1255. + targetNdx = globals.congaLine.length - 1;
  1256. + // add ourselves to the conga line
  1257. + globals.congaLine.push(gameObject);
  1258. + skinInstance.setAnimation('Walk');
  1259. + },
  1260. + update: () => {
  1261. + addHistory();
  1262. + // walk to the oldest point in the history
  1263. + const targetPos = targetHistory[0];
  1264. + const maxVelocity = globals.moveSpeed * globals.deltaTime;
  1265. + const deltaTurnSpeed = maxTurnSpeed * globals.deltaTime;
  1266. + const distance = aimTowardAndGetDistance(transform, targetPos, deltaTurnSpeed);
  1267. + const velocity = distance;
  1268. + transform.translateOnAxis(kForward, Math.min(velocity, maxVelocity));
  1269. + if (distance <= maxVelocity) {
  1270. + this.fsm.transition('follow');
  1271. + }
  1272. + },
  1273. + },
  1274. + follow: {
  1275. + update: () => {
  1276. + addHistory();
  1277. + // remove the oldest history and just put ourselves there.
  1278. + const targetPos = targetHistory.shift();
  1279. + transform.position.copy(targetPos);
  1280. + const deltaTurnSpeed = maxTurnSpeed * globals.deltaTime;
  1281. + aimTowardAndGetDistance(transform, targetHistory[0], deltaTurnSpeed);
  1282. + },
  1283. + },
  1284. + }, 'idle');
  1285. + }
  1286. + update() {
  1287. + this.fsm.update();
  1288. + }
  1289. }
  1290. ```
  1291. That was big chunk of code but it does what was described above.
  1292. Hopefully of you walk through each state it will be clear.
  1293. A few things we need to add. We need the player to add itself
  1294. to the globals so the animals can find it and we need to start the
  1295. conga line with the player's `GameObject`.
  1296. ```js
  1297. function init() {
  1298. ...
  1299. {
  1300. const gameObject = gameObjectManager.createGameObject(scene, 'player');
  1301. + globals.player = gameObject.addComponent(Player);
  1302. + globals.congaLine = [gameObject];
  1303. }
  1304. }
  1305. ```
  1306. We also need to compute a size for each model
  1307. ```js
  1308. function prepModelsAndAnimations() {
  1309. + const box = new THREE.Box3();
  1310. + const size = new THREE.Vector3();
  1311. Object.values(models).forEach(model => {
  1312. + box.setFromObject(model.gltf.scene);
  1313. + box.getSize(size);
  1314. + model.size = size.length();
  1315. const animsByName = {};
  1316. model.gltf.animations.forEach((clip) => {
  1317. animsByName[clip.name] = clip;
  1318. // Should really fix this in .blend file
  1319. if (clip.name === 'Walk') {
  1320. clip.duration /= 2;
  1321. }
  1322. });
  1323. model.animations = animsByName;
  1324. });
  1325. }
  1326. ```
  1327. And we need the player to record their size
  1328. ```js
  1329. class Player extends Component {
  1330. constructor(gameObject) {
  1331. super(gameObject);
  1332. const model = models.knight;
  1333. + globals.playerRadius = model.size / 2;
  1334. ```
  1335. Thinking about it now it would probably have been smarter
  1336. for the animals to just target the head of the conga line
  1337. instead of the player specifically. Maybe I'll come back
  1338. and change that later.
  1339. When I first started this I used just one radius for all animals
  1340. but of course that was no good as the pug is much smaller than the horse.
  1341. So I added the difference sizes but I wanted to be able to visualize
  1342. things. To do that I made a `StatusDisplayHelper` component.
  1343. I uses a `PolarGridHelper` to draw a circle around each character
  1344. and it uses html elements to let each character show some status using
  1345. the techniques covered in [the article on aligning html elements to 3D](threejs-align-html-elements-to-3d.html).
  1346. First we need to add some HTML to host these elements
  1347. ```html
  1348. <body>
  1349. <canvas id="c"></canvas>
  1350. <div id="ui">
  1351. <div id="left"><img src="resources/images/left.svg"></div>
  1352. <div style="flex: 0 0 40px;"></div>
  1353. <div id="right"><img src="resources/images/right.svg"></div>
  1354. </div>
  1355. <div id="loading">
  1356. <div>
  1357. <div>...loading...</div>
  1358. <div class="progress"><div id="progressbar"></div></div>
  1359. </div>
  1360. </div>
  1361. + <div id="labels"></div>
  1362. </body>
  1363. ```
  1364. And add some CSS for them
  1365. ```css
  1366. #labels {
  1367. position: absolute; /* let us position ourself inside the container */
  1368. left: 0; /* make our position the top left of the container */
  1369. top: 0;
  1370. color: white;
  1371. width: 100%;
  1372. height: 100%;
  1373. overflow: hidden;
  1374. pointer-events: none;
  1375. }
  1376. #labels>div {
  1377. position: absolute; /* let us position them inside the container */
  1378. left: 0; /* make their default position the top left of the container */
  1379. top: 0;
  1380. font-size: large;
  1381. font-family: monospace;
  1382. user-select: none; /* don't let the text get selected */
  1383. text-shadow: /* create a black outline */
  1384. -1px -1px 0 #000,
  1385. 0 -1px 0 #000,
  1386. 1px -1px 0 #000,
  1387. 1px 0 0 #000,
  1388. 1px 1px 0 #000,
  1389. 0 1px 0 #000,
  1390. -1px 1px 0 #000,
  1391. -1px 0 0 #000;
  1392. }
  1393. ```
  1394. Then here's the component
  1395. ```js
  1396. const labelContainerElem = document.querySelector('#labels');
  1397. class StateDisplayHelper extends Component {
  1398. constructor(gameObject, size) {
  1399. super(gameObject);
  1400. this.elem = document.createElement('div');
  1401. labelContainerElem.appendChild(this.elem);
  1402. this.pos = new THREE.Vector3();
  1403. this.helper = new THREE.PolarGridHelper(size / 2, 1, 1, 16);
  1404. gameObject.transform.add(this.helper);
  1405. }
  1406. setState(s) {
  1407. this.elem.textContent = s;
  1408. }
  1409. setColor(cssColor) {
  1410. this.elem.style.color = cssColor;
  1411. this.helper.material.color.set(cssColor);
  1412. }
  1413. update() {
  1414. const {pos} = this;
  1415. const {transform} = this.gameObject;
  1416. const {canvas} = globals;
  1417. pos.copy(transform.position);
  1418. // get the normalized screen coordinate of that position
  1419. // x and y will be in the -1 to +1 range with x = -1 being
  1420. // on the left and y = -1 being on the bottom
  1421. pos.project(globals.camera);
  1422. // convert the normalized position to CSS coordinates
  1423. const x = (pos.x * .5 + .5) * canvas.clientWidth;
  1424. const y = (pos.y * -.5 + .5) * canvas.clientHeight;
  1425. // move the elem to that position
  1426. this.elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  1427. }
  1428. }
  1429. ```
  1430. And we can then add them to the animals like this
  1431. ```js
  1432. class Animal extends Component {
  1433. constructor(gameObject, model) {
  1434. super(gameObject);
  1435. + this.helper = gameObject.addComponent(StateDisplayHelper, model.size);
  1436. ...
  1437. }
  1438. update() {
  1439. this.fsm.update();
  1440. + const dir = THREE.MathUtils.radToDeg(this.gameObject.transform.rotation.y);
  1441. + this.helper.setState(`${this.fsm.state}:${dir.toFixed(0)}`);
  1442. }
  1443. }
  1444. ```
  1445. While we're at it lets make it so we can turn them on/off using dat.GUI like
  1446. we've used else where
  1447. ```js
  1448. import * as THREE from './resources/three/r132/build/three.module.js';
  1449. import {OrbitControls} from './resources/threejs/r132/examples/jsm/controls/OrbitControls.js';
  1450. import {GLTFLoader} from './resources/threejs/r132/examples/jsm/loaders/GLTFLoader.js';
  1451. import * as SkeletonUtils from './resources/threejs/r132/examples/jsm/utils/SkeletonUtils.js';
  1452. +import {GUI} from '../3rdparty/dat.gui.module.js';
  1453. ```
  1454. ```js
  1455. +const gui = new GUI();
  1456. +gui.add(globals, 'debug').onChange(showHideDebugInfo);
  1457. +showHideDebugInfo();
  1458. const labelContainerElem = document.querySelector('#labels');
  1459. +function showHideDebugInfo() {
  1460. + labelContainerElem.style.display = globals.debug ? '' : 'none';
  1461. +}
  1462. +showHideDebugInfo();
  1463. class StateDisplayHelper extends Component {
  1464. ...
  1465. update() {
  1466. + this.helper.visible = globals.debug;
  1467. + if (!globals.debug) {
  1468. + return;
  1469. + }
  1470. ...
  1471. }
  1472. }
  1473. ```
  1474. And with that we get the kind of start of a game
  1475. {{{example url="../threejs-game-conga-line.html"}}}
  1476. Originally I set out to make a [snake game](https://www.google.com/search?q=snake+game)
  1477. where as you add animals to your line it gets harder because you need to avoid
  1478. crashing into them. I'd also have put some obstacles in the scene and maybe a fence or some
  1479. barrier around the perimeter.
  1480. Unfortunately the animals are long and thin. From above here's the zebra.
  1481. <div class="threejs_center"><img src="resources/images/zebra.png" style="width: 113px;"></div>
  1482. The code so far is using circle collisions which means if we had obstacles like a fence
  1483. then this would be considered a collision
  1484. <div class="threejs_center"><img src="resources/images/zebra-collisions.svg" style="width: 400px;"></div>
  1485. That's no good. Even animal to animal we'd have the same issue
  1486. I thought about writing a 2D rectangle to rectangle collision system but I
  1487. quickly realized it could really be a lot of code. Checking that 2 arbitrarily
  1488. oriented boxes overlap is not too much code and for our game with just a few
  1489. objects it might work but looking into it after a few objects you quickly start
  1490. needing to optimize the collision checking. First you might go through all
  1491. objects that can possibly collide with each other and check their bounding
  1492. spheres or bounding circles or their axially aligned bounding boxes. Once you
  1493. know which objects *might* be colliding then you need to do more work to check if
  1494. they are *actually* colliding. Often even checking the bounding spheres is too
  1495. much work and you need some kind of better spacial structure for the objects so
  1496. you can more quickly only check objects possibly near each other.
  1497. Then, once you write the code to check if 2 objects collide you generally want
  1498. to make a collision system rather than manually asking "do I collide with these
  1499. objects". A collision system emits events or calls callbacks in relation to
  1500. things colliding. The advantage is it can check all the collisions at once so no
  1501. objects get checked more than once where as if you manually call some "am I
  1502. colliding" function often objects will be checked more than once wasting time.
  1503. Making that collision system would probably not be more than 100-300 lines of
  1504. code for just checking arbitrarily oriented rectangles but it's still a ton more
  1505. code so it seemed best to leave it out.
  1506. Another solution would have been to try to find other characters that are
  1507. mostly circular from the top. Other humanoid characters for example instead
  1508. of animals in which case the circle checking might work animal to animal.
  1509. It would not work animal to fence, well we'd have to add circle to rectangle
  1510. checking. I thought about making the fence a fence of bushes or poles, something
  1511. circular but then I'd need probably 120 to 200 of them to surround the play area
  1512. which would run into the optimization issues mentioned above.
  1513. These are reasons many games use an existing solution. Often these solutions
  1514. are part of a physics library. The physical library needs to know if objects
  1515. collide with each other so on top of providing physics they can also be used
  1516. to detect collision.
  1517. If you're looking for a solution some of the three.js examples use
  1518. [ammo.js](https://github.com/kripken/ammo.js/) so that might be one.
  1519. One other solution might have been to place the obstacles on a grid
  1520. and try to make it so each animal and the player just need to look at
  1521. the grid. While that would be performant I felt that's best left as an exercise
  1522. for the reader 😜
  1523. One more thing, many game systems have something called [*coroutines*](https://www.google.com/search?q=coroutines).
  1524. Coroutines are routines that can pause while running and continue later.
  1525. Let's make the main character emit musical notes like they are leading
  1526. the line by singing. There are many ways we could implement this but for now
  1527. let's do it using coroutines.
  1528. First, here's a class to manage coroutines
  1529. ```js
  1530. function* waitSeconds(duration) {
  1531. while (duration > 0) {
  1532. duration -= globals.deltaTime;
  1533. yield;
  1534. }
  1535. }
  1536. class CoroutineRunner {
  1537. constructor() {
  1538. this.generatorStacks = [];
  1539. this.addQueue = [];
  1540. this.removeQueue = new Set();
  1541. }
  1542. isBusy() {
  1543. return this.addQueue.length + this.generatorStacks.length > 0;
  1544. }
  1545. add(generator, delay = 0) {
  1546. const genStack = [generator];
  1547. if (delay) {
  1548. genStack.push(waitSeconds(delay));
  1549. }
  1550. this.addQueue.push(genStack);
  1551. }
  1552. remove(generator) {
  1553. this.removeQueue.add(generator);
  1554. }
  1555. update() {
  1556. this._addQueued();
  1557. this._removeQueued();
  1558. for (const genStack of this.generatorStacks) {
  1559. const main = genStack[0];
  1560. // Handle if one coroutine removes another
  1561. if (this.removeQueue.has(main)) {
  1562. continue;
  1563. }
  1564. while (genStack.length) {
  1565. const topGen = genStack[genStack.length - 1];
  1566. const {value, done} = topGen.next();
  1567. if (done) {
  1568. if (genStack.length === 1) {
  1569. this.removeQueue.add(topGen);
  1570. break;
  1571. }
  1572. genStack.pop();
  1573. } else if (value) {
  1574. genStack.push(value);
  1575. } else {
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. this._removeQueued();
  1581. }
  1582. _addQueued() {
  1583. if (this.addQueue.length) {
  1584. this.generatorStacks.splice(this.generatorStacks.length, 0, ...this.addQueue);
  1585. this.addQueue = [];
  1586. }
  1587. }
  1588. _removeQueued() {
  1589. if (this.removeQueue.size) {
  1590. this.generatorStacks = this.generatorStacks.filter(genStack => !this.removeQueue.has(genStack[0]));
  1591. this.removeQueue.clear();
  1592. }
  1593. }
  1594. }
  1595. ```
  1596. It does things similar to `SafeArray` to make sure that it's safe to add or remove
  1597. coroutines while other coroutines are running. It also handles nested coroutines.
  1598. To make a coroutine you make a [JavaScript generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*).
  1599. A generator function is preceded by the keyword `function*` (the asterisk is important!)
  1600. Generator functions can `yield`. For example
  1601. ```js
  1602. function* countOTo9() {
  1603. for (let i = 0; i < 10; ++i) {
  1604. console.log(i);
  1605. yield;
  1606. }
  1607. }
  1608. ```
  1609. If we added this function to the `CoroutineRunner` above it would print
  1610. out each number, 0 to 9, once per frame or rather once per time we called `runner.update`.
  1611. ```js
  1612. const runner = new CoroutineRunner();
  1613. runner.add(count0To9);
  1614. while(runner.isBusy()) {
  1615. runner.update();
  1616. }
  1617. ```
  1618. Coroutines are removed automatically when they are finished.
  1619. To remove a coroutine early, before it reaches the end you need to keep
  1620. a reference to its generator like this
  1621. ```js
  1622. const gen = count0To9();
  1623. runner.add(gen);
  1624. // sometime later
  1625. runner.remove(gen);
  1626. ```
  1627. In any case, in the player let's use a coroutine to emit a note every half second to 1 second
  1628. ```js
  1629. class Player extends Component {
  1630. constructor(gameObject) {
  1631. ...
  1632. + this.runner = new CoroutineRunner();
  1633. +
  1634. + function* emitNotes() {
  1635. + for (;;) {
  1636. + yield waitSeconds(rand(0.5, 1));
  1637. + const noteGO = gameObjectManager.createGameObject(scene, 'note');
  1638. + noteGO.transform.position.copy(gameObject.transform.position);
  1639. + noteGO.transform.position.y += 5;
  1640. + noteGO.addComponent(Note);
  1641. + }
  1642. + }
  1643. +
  1644. + this.runner.add(emitNotes());
  1645. }
  1646. update() {
  1647. + this.runner.update();
  1648. ...
  1649. }
  1650. }
  1651. function rand(min, max) {
  1652. if (max === undefined) {
  1653. max = min;
  1654. min = 0;
  1655. }
  1656. return Math.random() * (max - min) + min;
  1657. }
  1658. ```
  1659. You can see we make a `CoroutineRunner` and we add an `emitNotes` coroutine.
  1660. That function will run forever, waiting 0.5 to 1 seconds and then creating a game object
  1661. with a `Note` component.
  1662. For the `Note` component first lets make a texture with a note on it and
  1663. instead of loading a note image let's make one using a canvas like we covered in [the article on canvas textures](threejs-canvas-textures.html).
  1664. ```js
  1665. function makeTextTexture(str) {
  1666. const ctx = document.createElement('canvas').getContext('2d');
  1667. ctx.canvas.width = 64;
  1668. ctx.canvas.height = 64;
  1669. ctx.font = '60px sans-serif';
  1670. ctx.textAlign = 'center';
  1671. ctx.textBaseline = 'middle';
  1672. ctx.fillStyle = '#FFF';
  1673. ctx.fillText(str, ctx.canvas.width / 2, ctx.canvas.height / 2);
  1674. return new THREE.CanvasTexture(ctx.canvas);
  1675. }
  1676. const noteTexture = makeTextTexture('♪');
  1677. ```
  1678. The texture we create above is white each means when we use it
  1679. we can set the material's color and get a note of any color.
  1680. Now that we have a noteTexture here's the `Note` component.
  1681. It uses `SpriteMaterial` and a `Sprite` like we covered in
  1682. [the article on billboards](threejs-billboards.html)
  1683. ```js
  1684. class Note extends Component {
  1685. constructor(gameObject) {
  1686. super(gameObject);
  1687. const {transform} = gameObject;
  1688. const noteMaterial = new THREE.SpriteMaterial({
  1689. color: new THREE.Color().setHSL(rand(1), 1, 0.5),
  1690. map: noteTexture,
  1691. side: THREE.DoubleSide,
  1692. transparent: true,
  1693. });
  1694. const note = new THREE.Sprite(noteMaterial);
  1695. note.scale.setScalar(3);
  1696. transform.add(note);
  1697. this.runner = new CoroutineRunner();
  1698. const direction = new THREE.Vector3(rand(-0.2, 0.2), 1, rand(-0.2, 0.2));
  1699. function* moveAndRemove() {
  1700. for (let i = 0; i < 60; ++i) {
  1701. transform.translateOnAxis(direction, globals.deltaTime * 10);
  1702. noteMaterial.opacity = 1 - (i / 60);
  1703. yield;
  1704. }
  1705. transform.parent.remove(transform);
  1706. gameObjectManager.removeGameObject(gameObject);
  1707. }
  1708. this.runner.add(moveAndRemove());
  1709. }
  1710. update() {
  1711. this.runner.update();
  1712. }
  1713. }
  1714. ```
  1715. All it does is setup a `Sprite`, then pick a random velocity and move
  1716. the transform at that velocity for 60 frames while fading out the note
  1717. by setting the material's [`opacity`](Material.opacity).
  1718. After the loop it the removes the transform
  1719. from the scene and the note itself from active gameobjects.
  1720. One last thing, let's add a few more animals
  1721. ```js
  1722. function init() {
  1723. ...
  1724. const animalModelNames = [
  1725. 'pig',
  1726. 'cow',
  1727. 'llama',
  1728. 'pug',
  1729. 'sheep',
  1730. 'zebra',
  1731. 'horse',
  1732. ];
  1733. + const base = new THREE.Object3D();
  1734. + const offset = new THREE.Object3D();
  1735. + base.add(offset);
  1736. +
  1737. + // position animals in a spiral.
  1738. + const numAnimals = 28;
  1739. + const arc = 10;
  1740. + const b = 10 / (2 * Math.PI);
  1741. + let r = 10;
  1742. + let phi = r / b;
  1743. + for (let i = 0; i < numAnimals; ++i) {
  1744. + const name = animalModelNames[rand(animalModelNames.length) | 0];
  1745. const gameObject = gameObjectManager.createGameObject(scene, name);
  1746. gameObject.addComponent(Animal, models[name]);
  1747. + base.rotation.y = phi;
  1748. + offset.position.x = r;
  1749. + offset.updateWorldMatrix(true, false);
  1750. + offset.getWorldPosition(gameObject.transform.position);
  1751. + phi += arc / r;
  1752. + r = b * phi;
  1753. }
  1754. ```
  1755. {{{example url="../threejs-game-conga-line-w-notes.html"}}}
  1756. You might be asking, why not use `setTimeout`? The problem with `setTimeout`
  1757. is it's not related to the game clock. For example above we made the maximum
  1758. amount of time allowed to elapse between frames to be 1/20th of a second.
  1759. Our coroutine system will respect that limit but `setTimeout` would not.
  1760. Of course we could have made a simple timer ourselves
  1761. ```js
  1762. class Player ... {
  1763. update() {
  1764. this.noteTimer -= globals.deltaTime;
  1765. if (this.noteTimer <= 0) {
  1766. // reset timer
  1767. this.noteTimer = rand(0.5, 1);
  1768. // create a gameobject with a note component
  1769. }
  1770. }
  1771. ```
  1772. And for this particular case that might have been better but as you add
  1773. more and things you'll get more and more variables added to your classes
  1774. where as with coroutines you can often just *fire and forget*.
  1775. Given our animal's simple states we could also have implemented them
  1776. with a coroutine in the form of
  1777. ```js
  1778. // pseudo code!
  1779. function* animalCoroutine() {
  1780. setAnimation('Idle');
  1781. while(playerIsTooFar()) {
  1782. yield;
  1783. }
  1784. const target = endOfLine;
  1785. setAnimation('Jump');
  1786. while(targetIsTooFar()) {
  1787. aimAt(target);
  1788. yield;
  1789. }
  1790. setAnimation('Walk')
  1791. while(notAtOldestPositionOfTarget()) {
  1792. addHistory();
  1793. aimAt(target);
  1794. yield;
  1795. }
  1796. for(;;) {
  1797. addHistory();
  1798. const pos = history.unshift();
  1799. transform.position.copy(pos);
  1800. aimAt(history[0]);
  1801. yield;
  1802. }
  1803. }
  1804. ```
  1805. This would have worked but of course as soon as our states were not so linear
  1806. we'd have had to switch to a `FiniteStateMachine`.
  1807. It also wasn't clear to me if coroutines should run independently of their
  1808. components. We could have made a global `CoroutineRunner` and put all
  1809. coroutines on it. That would make cleaning them up harder. As it is now
  1810. if the gameobject is removed all of its components are removed and
  1811. therefore the coroutine runners created are no longer called and it will
  1812. all get garbage collected. If we had global runner then it would be
  1813. the responsibility of each component to remove any coroutines it added
  1814. or else some other mechanism of registering coroutines with a particular
  1815. component or gameobject would be needed so that removing one removes the
  1816. others.
  1817. There are lots more issues a
  1818. normal game engine would deal with. As it is there is no order to how
  1819. gameobjects or their components are run. They are just run in the order added.
  1820. Many game systems add a priority so the order can be set or changed.
  1821. Another issue we ran into is the `Note` removing its gameobject's transform from the scene.
  1822. That seems like something that should happen in `GameObject` since it was `GameObject`
  1823. that added the transform in the first place. Maybe `GameObject` should have
  1824. a `dispose` method that is called by `GameObjectManager.removeGameObject`?
  1825. Yet another is how we're manually calling `gameObjectManager.update` and `inputManager.update`.
  1826. Maybe there should be a `SystemManager` which these global services can add themselves
  1827. and each service will have its `update` function called. In this way if we added a new
  1828. service like `CollisionManager` we could just add it to the system manager and not
  1829. have to edit the render loop.
  1830. I'll leave those kinds of issues up to you.
  1831. I hope this article has given you some ideas for your own game engine.
  1832. Maybe I should promote a game jam. If you click the *jsfiddle* or *codepen* buttons
  1833. above the last example they'll open in those sites ready to edit. Add some features,
  1834. Change the game to a pug leading a bunch of knights. Use the knight's rolling animation
  1835. as a bowling ball and make an animal bowling game. Make an animal relay race.
  1836. If you make a cool game post a link in the comments below.
  1837. <div class="footnotes">
  1838. [<a id="parented">1</a>]: technically it would still work if none of the parents have any translation, rotation, or scale <a href="#parented-backref">§</a>.
  1839. </div>