/** * Snake Food */ var gem = { start: function() { this.renderer.subImage = Math.floor(Math.random() * this.renderer.image.subImages); this.transform.scale.x = 0; this.transform.scale.y = 0; var scaleUpTween = new TWEEN.Tween(this.transform.scale); scaleUpTween.to({x: [1.5, 1], y: [1.5, 1]}, 800); scaleUpTween.easing(TWEEN.Easing.Back.Out); scaleUpTween.start(); this.shakeLength = 1000; this.shakes = 4; this.shakeAngle = 45; this.minShakeTimer = 5; this.maxShakeTimer = 20; this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer)); this.currentShakeTimer = 0; enemySnake.edibles.push(this); GameAudio.play("bubble"); }, update: function(deltaTime) { this.currentShakeTimer += deltaTime; if(this.currentShakeTimer >= this.shakeTimer) { this.shake(); this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer)); this.currentShakeTimer = 0; } }, onDestroy: function() { gemSpawner.currentActive--; enemySnake.edibles.remove(this); }, onCollision: function(other) { if (other.name == "snake" && !this.destroyed) { other.grow(1, -1); this.destroy(); } }, shake: function(callback, context) { var singleShakeTime = this.shakes / this.shakeLength; var shakeTween = new TWEEN.Tween(this.transform); var angles = []; var offset = Math.round(Math.random() * 5); for (var i = 0; i < this.shakes; i++) { var intensiy = 1 - (i / this.shakes); // Direction of the shake (is 'i' odd?) var dir = !((i + offset) % 2)? -1 : 1; var angleRad = this.shakeAngle; var angle = angleRad * dir * intensiy; angles.push(angle); } angles.push(0); shakeTween.to({rotation: angles}, this.shakeLength); shakeTween.easing(TWEEN.Easing.Quadratic.InOut); if (callback) { shakeTween.onComplete.addOnce(callback, context); } shakeTween.start(); } } /** * Gem Spawner spawns gems in the world */ var gemSpawner = { start: function() { gemSpawner.currentActive = 0; this.maxGems = 7; for (var i = 0; i < this.maxGems; i++) { this.spawnGem(i * 200, i == this.maxGems - 1); } this.spawnInUpdate = false; this.allGems = []; }, spawnGem: function(delay, enableSpawningAfter) { delay = delay || 0; if (gemSpawner.currentActive < this.maxGems) { gemSpawner.currentActive++; timingUtil.delay(delay, function() { var x = 100 + Math.random() * (GameWindow.width - 200); var y = 100 + Math.random() * (GameWindow.height - 200); this.allGems.push(GameObject.create(gem, x, y, s_gem, 0)); this.allGems = this.allGems.filter(function(item) { return !item.destroyed; }); if (enableSpawningAfter) { this.spawnInUpdate = true; } }, this); } }, onDestroy: function() { for (var i = 0; i < this.allGems.length; i++) { this.allGems[i].destroy(); } }, update: function(deltaTime) { if (this.spawnInUpdate) { this.spawnGem(500 + (Math.random() * 500)); } } } /*** * All images used in the game are initialized here */ var s_head = new image("images/head.png", 4, 4); var s_orb = new image("images/orb.png", 4, 4); var s_gem = new image("images/gems.png", 72, 6); /** * Main script */ window.onload = init; function DoInheritance() { //resultclass = GameObject.inherit(baseclass, childclass); } function init() { DoInheritance(); //Game.initialize(xpos, ypos, width, height, fps, callback) // -2 to account for borders var width = window.innerWidth - 2; var height = window.innerHeight - 2; Game.initialize(0, 0, width, height, gameStart); } function gameStart() { GameWindow.backgroundColor = {r:100, g:149, b:237}; Game.setUpdateFunction(update); // Let the game engine know we want to link our 'update()' to the game's update step. GameAudio.init(sounds, soundsFolder); p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500); } function update(deltaTime) { } /** * All sounds in the game */ var soundsFolder = "sounds"; var sounds = [ { name: "bubble", volume: 1 }, { name: "glass", volume: 0.9 }, { name: "tap", volume: 1 }, { name: "bite", volume: 1 }, { name: "pop", volume: 0.1 } ]; //List of all sound files as strings (do not include file extension)