r3-custom-code/21g30t1e75.js

1989 lines
56 KiB
JavaScript

if (this.parentEntity === data.entity) {
console.log('snake entity loaded');
} else {
return;
}
/**
* Defines
*/
R3.CustomCode.BODY_TYPE_NORMAL = 0x1;
R3.CustomCode.BODY_TYPE_TAIL = 0x2;
R3.CustomCode.BODY_TYPE_CORNER = 0x3;
R3.CustomCode.GRID_WIDTH = 11;
R3.CustomCode.GRID_HEIGHT = 10;
R3.CustomCode.GRID_OFFSET_X = 1;
R3.CustomCode.GRID_OFFSET_Y = 8;
R3.CustomCode.SPEED_INITIAL = 1;
R3.CustomCode.ORIENTATION_UP = 0;
R3.CustomCode.ORIENTATION_LEFT = 1;
R3.CustomCode.ORIENTATION_DOWN = 2;
R3.CustomCode.ORIENTATION_RIGHT = 3;
R3.CustomCode.FOOD_SPEED_INITIAL = 3;
R3.CustomCode.MAX_FOOD_ITEMS = 6;
R3.CustomCode.POWERUP_WAIT_TIME_MIN = 5;
R3.CustomCode.POWERUP_WAIT_TIME_MAX = 10;
R3.CustomCode.POWERUP_DURATION = 7;
R3.CustomCode.POWERUP_SPEED = 0;
R3.CustomCode.POWERUP_LIFE = 1;
R3.CustomCode.POWERUP_SLOW = 2;
R3.CustomCode.MAX_POWERUP_ITEMS = 3;
R3.CustomCode.SPEED_GRAIN = 0.8 / 20;
R3.CustomCode.POWERUP_SPEED_GRAIN = 0.8 / 5;
R3.CustomCode.POWERUP_ANIMATION_SPEED_GRAIN = 8 / 5;
R3.CustomCode.MAX_SPEED = 0.2;
R3.CustomCode.MIN_SPEED = R3.CustomCode.SPEED_INITIAL;
R3.CustomCode.ANIMATION_SPEED_GRAIN = 8 / 20;
R3.CustomCode.MIN_ANIMATION_SPEED = 2;
R3.CustomCode.MAX_ANIMATION_SPEED = 10;
R3.CustomCode.SPEED_INCREASE_INTERVAL = 5;
R3.CustomCode.FOOD_BACON = 0;
R3.CustomCode.FOOD_CHEESE = 1;
R3.CustomCode.FOOD_ONION = 2;
R3.CustomCode.FOOD_ONION_RING = 3;
R3.CustomCode.FOOD_PATTY = 4;
R3.CustomCode.FOOD_TOMATO = 5;
R3.CustomCode.BODY_TYPE_BREAD_BACON = 0;
R3.CustomCode.BODY_TYPE_BREAD_CHEESE = 1;
R3.CustomCode.BODY_TYPE_BREAD_ONION = 2;
R3.CustomCode.BODY_TYPE_BREAD_ONION_RING = 3;
R3.CustomCode.BODY_TYPE_BREAD_PATTY = 4;
R3.CustomCode.BODY_TYPE_BREAD_TOMATO = 5;
R3.CustomCode.BODY_TYPE_BREAD_HEAD = 6;
R3.CustomCode.BODY_TYPE_BREAD_TAIL = 7;
R3.CustomCode.BODY_TYPE_BREAD_CORNER = 8;
R3.CustomCode.OBJECT_TYPE_NONE = 0;
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY = 1;
R3.CustomCode.OBJECT_TYPE_FOOD = 2;
R3.CustomCode.OBJECT_TYPE_POWERUP = 3;
this.extendSpeedGrain = (R3.CustomCode.SPEED_INITIAL - R3.CustomCode.MAX_SPEED) / (R3.CustomCode.GRID_WIDTH * R3.CustomCode.GRID_HEIGHT);
/**
* Get runtime
*/
this.runtime = R3.Utils.GetRuntime();
/**
* Custom Code Components
*/
this.beforeRender = R3.EntityManager.Instance.findComponentById('zjq6ach3jt');
this.keyUp = R3.EntityManager.Instance.findComponentById('306204wy29');
this.touchStart = R3.EntityManager.Instance.findComponentById('0cw20zm4g7');
this.touchEnd = R3.EntityManager.Instance.findComponentById('0j2wxrtdf2');
this.mouseDown = R3.EntityManager.Instance.findComponentById('gzewp2awcf');
this.mouseUp = R3.EntityManager.Instance.findComponentById('ckplg6wqnl');
/**
* Geometries
*/
this.geometryBody = R3.EntityManager.Instance.findComponentById('8f5q7k5ozp');
R3.CustomCode.BODY_SCALE_X = this.geometryBody.width;
R3.CustomCode.BODY_SCALE_Y = this.geometryBody.height;
/**
* Meshes
*/
this.meshPauseButton = R3.EntityManager.Instance.findComponentById('f5xmype7dq');
this.meshSoundButton = R3.EntityManager.Instance.findComponentById('0eo0kat3fb');
this.meshDownButton = R3.EntityManager.Instance.findComponentById('2eva6brga6');
this.meshUpButton = R3.EntityManager.Instance.findComponentById('q3ra6x84d8');
this.meshRightButton = R3.EntityManager.Instance.findComponentById('z0gwmplslj');
this.meshLeftButton = R3.EntityManager.Instance.findComponentById('e69gb1028e');
this.meshPauseOverlay = R3.EntityManager.Instance.findComponentById('gjhshqb1ks');
this.buttons = [
this.meshPauseButton,
this.meshSoundButton,
this.meshDownButton,
this.meshUpButton,
this.meshRightButton,
this.meshLeftButton,
];
/**
* Materials
*/
this.materialTomato = R3.EntityManager.Instance.findComponentById('merwtvkm1t');
this.materialBacon = R3.EntityManager.Instance.findComponentById('x5ffcl6ojc');
this.materialCheese = R3.EntityManager.Instance.findComponentById('0rxqy5eddx');
this.materialOnionRing = R3.EntityManager.Instance.findComponentById('gz8zex5xug');
this.materialPatty = R3.EntityManager.Instance.findComponentById('f7y8nurbcv');
this.materialOnion = R3.EntityManager.Instance.findComponentById('im3tsuzwrp');
this.materialBreadBacon = R3.EntityManager.Instance.findComponentById('wn31yqi6lc');
this.materialBreadCheese = R3.EntityManager.Instance.findComponentById('i22ixqizge');
this.materialBreadOnion = R3.EntityManager.Instance.findComponentById('69sybcj08d');
this.materialBreadOnionRing = R3.EntityManager.Instance.findComponentById('kvesyjtr2v');
this.materialBreadPatty = R3.EntityManager.Instance.findComponentById('k6axym9bu5');
this.materialBreadTomato = R3.EntityManager.Instance.findComponentById('9wxuzzz5co');
this.materialBreadHead = R3.EntityManager.Instance.findComponentById('heu4f7zzuh');
this.materialBreadTail = R3.EntityManager.Instance.findComponentById('mm2yq9rmpf');
this.materialBreadCorner = R3.EntityManager.Instance.findComponentById('ju9r1bw6cb');
this.materialPowerupSpeed = R3.EntityManager.Instance.findComponentById('fb32uutj9q');
this.materialPowerupLife = R3.EntityManager.Instance.findComponentById('6tld70a6zu');
this.materialPowerupSlow = R3.EntityManager.Instance.findComponentById('ulgntj8nta');
/**
* Images
*/
this.imagePowerupSpeed = R3.EntityManager.Instance.findComponentById('3mly5q145f');
this.imagePowerupLife = R3.EntityManager.Instance.findComponentById('6ezbfrssvx');
this.imagePowerupSlow = R3.EntityManager.Instance.findComponentById('nhd62hn7sa');
this.imageHeart = R3.EntityManager.Instance.findComponentById('oa5xl4n05z');
this.imageHeartGrey = R3.EntityManager.Instance.findComponentById('bh4miaqzwe');
this.imagePauseButton = R3.EntityManager.Instance.findComponentById('x0ewaqg5fu');
this.imageSoundButton = R3.EntityManager.Instance.findComponentById('w8sppso1z4');
this.imageDownButton = R3.EntityManager.Instance.findComponentById('t0w8hmodl3');
this.imageUpButton = R3.EntityManager.Instance.findComponentById('4v63jgi7t5');
this.imageRightButton = R3.EntityManager.Instance.findComponentById('21o4zucc3c');
this.imageLeftButton = R3.EntityManager.Instance.findComponentById('qkq4gs682q');
R3.CustomCode.prototype.createButtonAlphas = function(image) {
var pixels = image.getPixelData();
image.alphas = [];
for (i = 0; i < pixels.length; i += 4) {
image.alphas.push(pixels[i + 3]);
}
}.bind(this)
this.createButtonAlphas(this.imagePauseButton);
this.createButtonAlphas(this.imageSoundButton);
this.createButtonAlphas(this.imageDownButton);
this.createButtonAlphas(this.imageUpButton);
this.createButtonAlphas(this.imageRightButton);
this.createButtonAlphas(this.imageLeftButton);
/**
* Other Objects (Scene)
*/
this.scene = R3.EntityManager.Instance.findComponentById('pllp034hsj');
this.camera = R3.EntityManager.Instance.findComponentById('u8eze0uajt');
this.raycaster = R3.EntityManager.Instance.findComponentById('ftkhrkfqnu');
this.animation = R3.EntityManager.Instance.findComponentById('8kb7utb2fn');
this.animationRotation = R3.EntityManager.Instance.findComponentById('z628kythyn');
this.particleEnginePickle = R3.EntityManager.Instance.findComponentById('ddtkiu8aaa');
this.particlePickle = R3.EntityManager.Instance.findComponentById('8lx1d9tnbf');
R3.CustomCode.EXPLODE_LIFETIME = this.particlePickle.lifeTime;
this.canvasHUD = R3.EntityManager.Instance.findComponentById('lrh7junmhm');
this.textureHUD = R3.EntityManager.Instance.findComponentById('zrjg7mqkxi');
this.animation.translationSpeed = R3.CustomCode.MIN_ANIMATION_SPEED;
/**
* Game objects
*/
this.snake = [];
this.grid = [[]];
this.speed = R3.CustomCode.SPEED_INITIAL;
this.advanceTime = 0;
this.foodTime = 0;
this.foodSpeed = R3.CustomCode.FOOD_SPEED_INITIAL;
this.food = [];
this.powerupTime = 0;
this.nextPowerupTime = R3.Utils.GetRandomIntInclusive(
R3.CustomCode.POWERUP_WAIT_TIME_MIN,
R3.CustomCode.POWERUP_WAIT_TIME_MAX
);
this.powerups = [];
this.explodeTime = 0;
this.speedTimer = 0;
/**
* Orientation is 0, 1, 2 or 3, (up, left, down, right) -
* This is also the amount we need to multiply with PI to get the mesh rotation
* @type {{direction: {x: number, y: number}, orientation: number}}
*/
this.state = {
orientation : 0,
turning : false,
flip : 0,
eating : false,
exploding : false,
lives : 3,
getReady : false,
message : null,
gameOver : false,
muted : false,
paused : false,
score : 0,
highscore : 0
};
R3.CustomCode.prototype.advanceSpeedTimer = function(delta) {
this.speedTimer += delta;
if (this.speedTimer < R3.CustomCode.SPEED_INCREASE_INTERVAL) {
return;
}
this.speedTimer = 0;
this.speed -= R3.CustomCode.SPEED_GRAIN;
if (this.speed < R3.CustomCode.MAX_SPEED) {
this.speed = R3.CustomCode.MAX_SPEED;
}
this.animation.translationSpeed += R3.CustomCode.ANIMATION_SPEED_GRAIN;
if (this.animation.translationSpeed > R3.CustomCode.MAX_ANIMATION_SPEED) {
this.animation.translationSpeed = R3.CustomCode.MAX_ANIMATION_SPEED;
}
console.log(this.animation.translationSpeed + ':' + this.speed );
}.bind(this)
R3.CustomCode.prototype.displayHUD = function() {
this.canvasHUD.clear();
this.canvasHUD.line(50, 38, 462, 38, 2, "#000000");
var highscore = R3.Utils.PaddedText(5, '0', this.state.highscore + '') + 'cm';
this.canvasHUD.text('hiscore ' + highscore, 50, 32, '15px Pixeled', '#ffffff');
var score = R3.Utils.PaddedText(5, '0', this.state.score + '') + 'cm';
this.canvasHUD.text(score, 356, 32, '15px Pixeled', '#ffffff');
var images = [];
if (this.state.lives === 3) {
images.push(this.imageHeart.instance);
images.push(this.imageHeart.instance);
images.push(this.imageHeart.instance);
} else if (this.state.lives === 2) {
images.push(this.imageHeart.instance);
images.push(this.imageHeart.instance);
images.push(this.imageHeartGrey.instance);
} else if (this.state.lives === 1) {
images.push(this.imageHeart.instance);
images.push(this.imageHeartGrey.instance);
images.push(this.imageHeartGrey.instance);
} else if (this.state.lives <= 0) {
images.push(this.imageHeartGrey.instance);
images.push(this.imageHeartGrey.instance);
images.push(this.imageHeartGrey.instance);
}
this.canvasHUD.image(images[0], 262, 5, 30, 30);
this.canvasHUD.image(images[1], 292, 5, 30, 30);
this.canvasHUD.image(images[2], 322, 5, 30, 30);
this.canvasHUD.line(50, 456, 462, 456, 2, "#000000");
this.canvasHUD.text('xtra', 110, 480, '12px Pixeled', '#ffffff');
this.canvasHUD.text('speed', 110, 500, '12px Pixeled', '#ffffff');
this.canvasHUD.text('xtra', 265, 480, '12px Pixeled', '#ffffff');
this.canvasHUD.text('life', 265, 500, '12px Pixeled', '#ffffff');
this.canvasHUD.text('xtra', 420, 480, '12px Pixeled', '#ffffff');
this.canvasHUD.text('slow', 420, 500, '12px Pixeled', '#ffffff');
if (this.state.message) {
this.canvasHUD.text(
this.state.message.text,
this.state.message.x,
this.state.message.y,
'30px Pixeled',
'#ffffff'
);
}
this.canvasHUD.image(this.imagePowerupSpeed.instance, 50, 460, 48, 48);
this.canvasHUD.image(this.imagePowerupLife.instance, 205, 460, 48, 48);
this.canvasHUD.image(this.imagePowerupSlow.instance, 360, 460, 48, 48);
this.textureHUD.instance.needsUpdate = true;
}.bind(this)
R3.CustomCode.prototype.waitReload = function(delta) {
this.explodeTime += delta;
if (this.explodeTime < R3.CustomCode.EXPLODE_LIFETIME) {
return;
}
this.explodeTime = 0;
/**
* We need to revert to a good state
*/
console.warn('reverting to good state');
this.state.exploding = false;
if (this.state.lives <= 0) {
this.restore();
this.gameOver(false);
return;
}
this.displayHUD();
this.restore();
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Get Ready'
}
);
this.state.getReady = true;
window.setTimeout(
function(){
this.state.message = null;
this.displayHUD();
}.bind(this),
2600
);
window.setTimeout(
function(){
this.state.message = {
text: "GO!",
x: 222,
y: 256
};
this.displayHUD();
this.state.getReady = false;
}.bind(this),
1950
);
window.setTimeout(
function(){
this.state.message = {
text: "1",
x: 253,
y: 256
};
this.displayHUD();
}.bind(this),
1300
);
window.setTimeout(
function(){
this.state.message = {
text: "2",
x: 244,
y: 256
};
this.displayHUD();
}.bind(this),
650
);
this.state.message = {
text: "3",
x: 244,
y: 256
};
this.displayHUD();
}.bind(this)
R3.CustomCode.prototype.visualizeGrid = function () {
return;
if (this.noneMesh) {
this.scene.instance.remove(this.noneMesh);
}
if (this.foodMesh) {
this.scene.instance.remove(this.foodMesh);
}
if (this.powerupMesh) {
this.scene.instance.remove(this.powerupMesh);
}
if (this.snakeMesh) {
this.scene.instance.remove(this.snakeMesh);
}
this.noneGeometry = new THREE.Geometry();
this.foodGeometry = new THREE.Geometry();
this.powerupGeometry = new THREE.Geometry();
this.snakeGeometry = new THREE.Geometry();
this.grid.map(
function(x, xIndex) {
x.map(
function(object, yIndex) {
if (object.objectType === R3.CustomCode.OBJECT_TYPE_NONE) {
this.noneGeometry.vertices.push(
new THREE.Vector3(
(xIndex * R3.CustomCode.BODY_SCALE_X) + R3.CustomCode.GRID_OFFSET_X,
(yIndex * R3.CustomCode.BODY_SCALE_Y) + R3.CustomCode.GRID_OFFSET_Y,
5
)
);
}
if (object.objectType === R3.CustomCode.OBJECT_TYPE_SNAKE_BODY) {
this.snakeGeometry.vertices.push(
new THREE.Vector3(
(xIndex * R3.CustomCode.BODY_SCALE_X) + R3.CustomCode.GRID_OFFSET_X,
(yIndex * R3.CustomCode.BODY_SCALE_Y) + R3.CustomCode.GRID_OFFSET_Y,
5
)
);
}
if (object.objectType === R3.CustomCode.OBJECT_TYPE_FOOD) {
this.foodGeometry.vertices.push(
new THREE.Vector3(
(xIndex * R3.CustomCode.BODY_SCALE_X) + R3.CustomCode.GRID_OFFSET_X,
(yIndex * R3.CustomCode.BODY_SCALE_Y) + R3.CustomCode.GRID_OFFSET_Y,
5
)
);
}
if (object.objectType === R3.CustomCode.OBJECT_TYPE_POWERUP) {
this.powerupGeometry.vertices.push(
new THREE.Vector3(
(xIndex * R3.CustomCode.BODY_SCALE_X) + R3.CustomCode.GRID_OFFSET_X,
(yIndex * R3.CustomCode.BODY_SCALE_Y) + R3.CustomCode.GRID_OFFSET_Y,
5
)
);
}
}.bind(this)
)
}.bind(this)
);
var noneMaterial = new THREE.PointsMaterial({color: 0xffffff, size: 0.1});
var foodMaterial = new THREE.PointsMaterial({color: 0x00ff00, size: 0.1});
var snakeMaterial = new THREE.PointsMaterial({color: 0x0000ff, size: 0.1});
var powerupMaterial = new THREE.PointsMaterial({color: 0xff0000, size: 0.1});
this.noneMesh = new THREE.Points(this.noneGeometry, noneMaterial);
this.foodMesh = new THREE.Points(this.foodGeometry, foodMaterial);
this.powerupMesh = new THREE.Points(this.powerupGeometry, powerupMaterial);
this.snakeMesh = new THREE.Points(this.snakeGeometry, snakeMaterial);
this.scene.instance.add(this.noneMesh);
this.scene.instance.add(this.foodMesh);
this.scene.instance.add(this.powerupMesh);
this.scene.instance.add(this.snakeMesh);
}.bind(this)
R3.CustomCode.prototype.createGameMesh = function(material, visible) {
if (R3.Utils.UndefinedOrNull(visible)) {
visible = true;
}
var mesh = new R3.D3.Mesh(
this.runtime.graphics,
{
geometry : this.geometryBody,
materials : [material],
visible : visible,
useQuaternion : false
}
)
this.scene.addClone(mesh);
return mesh;
}.bind(this)
/**
* R3.CustomCode.Object
* @param objectType
* @param type
* @param mesh
* @param position
* @constructor
*/
R3.CustomCode.GameObject = function(
objectType,
type,
mesh,
position
) {
if (R3.Utils.UndefinedOrNull(objectType)) {
objectType = R3.CustomCode.OBJECT_TYPE_NONE;
}
this.objectType = objectType;
if (R3.Utils.UndefinedOrNull(type)) {
type = -1;
}
this.type = type;
if (R3.Utils.UndefinedOrNull(mesh)) {
mesh = null;
}
this.mesh = mesh;
if (R3.Utils.UndefinedOrNull(position)) {
position = null;
}
this.position = position;
}
/**
* Disposes of the mesh object and resets this object to its defaults
*/
R3.CustomCode.GameObject.prototype.dispose = function(removeMesh) {
if (R3.Utils.UndefinedOrNull(removeMesh)) {
removeMesh = true;
}
if (this.mesh && removeMesh) {
this.mesh.geometry = null;
this.mesh.materials = null;
this.mesh.remove();
this.mesh = null;
}
this.objectType = R3.CustomCode.OBJECT_TYPE_NONE;
this.type = -1;
this.position = null;
};
/**
* Applies the object position to the mesh taking into account the offset and scale of the body
* @param updateInstance
*/
R3.CustomCode.GameObject.prototype.applyToMesh = function(updateInstance) {
if (this.mesh) {
this.mesh.position.x = (this.position.x * R3.CustomCode.BODY_SCALE_X) + R3.CustomCode.GRID_OFFSET_X;
this.mesh.position.y = (this.position.y * R3.CustomCode.BODY_SCALE_Y) + R3.CustomCode.GRID_OFFSET_Y;
this.mesh.position.z = 0.1;
if (updateInstance) {
this.mesh.updateInstance('position');
}
}
};
/**
* Scans through the grid, gets all empty positions, and chooses a random one
* @type {function(this:snakeEntityLoaded)}
*/
R3.CustomCode.prototype.getFreeGridPosition = function() {
var positions = [];
this.grid.map(
function(row, x) {
row.map(
function(object, y) {
if (object.objectType === R3.CustomCode.OBJECT_TYPE_NONE) {
positions.push(
{
x : x,
y : y
}
)
}
}
);
}
);
if (positions.length === 0) {
return null;
}
var index = R3.Utils.GetRandomInt(0, positions.length);
return positions[index];
}.bind(this);
/**
*
* @type {function(this:snakeEntityLoaded)}
*/
R3.CustomCode.prototype.createGameObject = function(
objectType,
type,
position,
orientation
) {
if (R3.Utils.UndefinedOrNull(type)) {
type = null;
}
var mesh = null;
var array = null;
if (objectType === R3.CustomCode.OBJECT_TYPE_POWERUP) {
type = R3.Utils.GetRandomIntInclusive(0, 2);
switch (type) {
case R3.CustomCode.POWERUP_SPEED :
mesh = this.createGameMesh(this.materialPowerupSpeed);
break;
case R3.CustomCode.POWERUP_LIFE :
mesh = this.createGameMesh(this.materialPowerupLife);
break;
case R3.CustomCode.POWERUP_SLOW :
mesh = this.createGameMesh(this.materialPowerupSlow);
break;
default:
throw new Error('unhandled power up type : ' + type)
}
/**
* We apply a scale to the powerupss too - since they appear too big when normal
*/
mesh.scale.x = 0.8;
mesh.scale.y = 0.8;
mesh.updateInstance('scale');
array = this.powerups;
}
if (objectType === R3.CustomCode.OBJECT_TYPE_FOOD) {
type = R3.Utils.GetRandomIntInclusive(0, 5);
switch (type) {
case R3.CustomCode.FOOD_BACON :
mesh = this.createGameMesh(this.materialBacon);
break;
case R3.CustomCode.FOOD_CHEESE :
mesh = this.createGameMesh(this.materialCheese);
break;
case R3.CustomCode.FOOD_ONION :
mesh = this.createGameMesh(this.materialOnion);
break;
case R3.CustomCode.FOOD_ONION_RING :
mesh = this.createGameMesh(this.materialOnionRing);
break;
case R3.CustomCode.FOOD_PATTY :
mesh = this.createGameMesh(this.materialPatty);
break;
case R3.CustomCode.FOOD_TOMATO :
mesh = this.createGameMesh(this.materialTomato);
break;
default :
throw new Error('unhandled food type : ' + type)
}
/**
* We apply a scale to the food meshes too - since they appear too big when normal
*/
mesh.scale.x = 0.8;
mesh.scale.y = 0.8;
mesh.updateInstance('scale');
array = this.food;
}
var isTail = false;
if (objectType === R3.CustomCode.OBJECT_TYPE_SNAKE_BODY) {
switch (type) {
case R3.CustomCode.BODY_TYPE_BREAD_BACON :
mesh = this.createGameMesh(this.materialBreadBacon);
break;
case R3.CustomCode.BODY_TYPE_BREAD_CHEESE :
mesh = this.createGameMesh(this.materialBreadCheese);
break;
case R3.CustomCode.BODY_TYPE_BREAD_ONION :
mesh = this.createGameMesh(this.materialBreadOnion);
break;
case R3.CustomCode.BODY_TYPE_BREAD_ONION_RING :
mesh = this.createGameMesh(this.materialBreadOnionRing);
break;
case R3.CustomCode.BODY_TYPE_BREAD_PATTY :
mesh = this.createGameMesh(this.materialBreadPatty);
break;
case R3.CustomCode.BODY_TYPE_BREAD_TOMATO :
mesh = this.createGameMesh(this.materialBreadTomato);
break;
case R3.CustomCode.BODY_TYPE_BREAD_HEAD :
mesh = this.createGameMesh(this.materialBreadHead);
break;
case R3.CustomCode.BODY_TYPE_BREAD_TAIL :
mesh = this.createGameMesh(this.materialBreadTail);
isTail = true;
break;
case R3.CustomCode.BODY_TYPE_BREAD_CORNER :
mesh = this.createGameMesh(this.materialBreadCorner);
break;
default :
throw new Error('unhandled body type : ' + type)
}
array = this.snake;
}
var gameObject = null;
switch (objectType) {
case R3.CustomCode.OBJECT_TYPE_FOOD:
case R3.CustomCode.OBJECT_TYPE_POWERUP:
gameObject = new R3.CustomCode.GameObject(
objectType,
type,
mesh,
position
)
gameObject.applyToMesh(true);
break;
case R3.CustomCode.OBJECT_TYPE_SNAKE_BODY:
gameObject = new R3.CustomCode.SnakeBody(
type,
mesh,
position,
orientation,
null,
null,
isTail
)
gameObject.applyToMesh(true);
this.animation.meshes.push(gameObject.mesh);
break;
default:
throw new Error('unhandled object type: ' + objectType);
}
/**
* Update the grid with this object
*/
var currentGameObject = this.grid[gameObject.position.x][gameObject.position.y];
/**
* If there is a powerup at this position - remove it
*/
if (currentGameObject.objectType === R3.CustomCode.OBJECT_TYPE_POWERUP) {
this.powerups.splice(
this.powerups.indexOf(currentGameObject),
1
);
}
/**
* If there is food at this position - remove it
*/
if (currentGameObject.objectType === R3.CustomCode.OBJECT_TYPE_FOOD) {
this.food.splice(
this.food.indexOf(currentGameObject),
1
);
}
/**
* If there is an existing snake body at this position - explode!
*/
if (currentGameObject.objectType === R3.CustomCode.OBJECT_TYPE_SNAKE_BODY) {
console.warn('should not reach this code if collision detection is perfect');
this.explode(currentGameObject.position);
return gameObject;
}
this.grid[gameObject.position.x][gameObject.position.y].dispose();
this.grid[gameObject.position.x][gameObject.position.y] = gameObject;
/**
* Save a reference to this object to the specific object array
*/
if (
gameObject.objectType === R3.CustomCode.OBJECT_TYPE_POWERUP ||
gameObject.objectType === R3.CustomCode.OBJECT_TYPE_FOOD
) {
/**
* Food and powerups automatically get added, snake bodys need careful insertion
*/
array.push(gameObject);
}
/**
* If we have too many objects - dispose of the first on in the queue (fifo)
*/
if (this.food.length > R3.CustomCode.MAX_FOOD_ITEMS) {
gameObject = this.food.shift();
this.grid[gameObject.position.x][gameObject.position.y].dispose();
}
if (this.powerups.length > R3.CustomCode.MAX_POWERUP_ITEMS) {
gameObject = this.powerups.shift();
this.grid[gameObject.position.x][gameObject.position.y].dispose();
}
this.visualizeGrid();
return gameObject;
}.bind(this);
R3.CustomCode.prototype.createPowerup = function(delta) {
if (this.state.exploding) {
return;
}
this.powerupTime += delta;
if (this.powerupTime > this.nextPowerupTime) {
this.powerupTime = 0;
} else {
return;
}
var position = this.getFreeGridPosition();
if (position === null) {
this.gameOver(true);
} else {
this.createGameObject(
R3.CustomCode.OBJECT_TYPE_POWERUP,
null,
position
);
}
}.bind(this);
R3.CustomCode.prototype.createFood = function(delta) {
if (this.state.exploding) {
return;
}
this.foodTime += delta;
if (this.foodTime > this.foodSpeed) {
this.foodTime = 0;
} else {
return;
}
var position = this.getFreeGridPosition();
if (position === null) {
this.gameOver(true);
} else {
this.createGameObject(
R3.CustomCode.OBJECT_TYPE_FOOD,
null,
position
);
}
}.bind(this);
R3.CustomCode.prototype.gameOver = function(won) {
this.state.gameOver = true;
if (won) {
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Game Over Won'
}
);
this.state.message = {
text: "YOU WON!",
x: 152,
y: 256
};
} else {
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Game Over Lost'
}
);
this.state.message = {
text: "GAME OVER",
x: 127,
y: 256
};
}
this.displayHUD();
}.bind(this)
/**
* R3.CustomCode.SnakeBody
* @param type
* @param mesh
* @param position
* @param orientation
* @param flip
* @param backupMesh
* @param isTail
* @property animation
* @constructor
*/
R3.CustomCode.SnakeBody = function(
type,
mesh,
position,
orientation,
flip,
backupMesh,
isTail
) {
R3.CustomCode.GameObject.call(
this,
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
type,
mesh,
position
);
if (R3.Utils.UndefinedOrNull(orientation)) {
orientation = R3.CustomCode.ORIENTATION_UP;
}
this.orientation = orientation;
if (R3.Utils.UndefinedOrNull(flip)) {
flip = 0;
}
this.flip = flip;
if (R3.Utils.UndefinedOrNull(backupMesh)) {
backupMesh = null;
}
this.backupMesh = backupMesh;
if (R3.Utils.UndefinedOrNull(isTail)) {
isTail = false;
}
this.isTail = isTail;
};
R3.CustomCode.SnakeBody.prototype = Object.create(R3.CustomCode.GameObject.prototype);
R3.CustomCode.SnakeBody.prototype.constructor = R3.CustomCode.GameObject;
R3.CustomCode.SnakeBody.prototype.dispose = function() {
R3.CustomCode.GameObject.prototype.dispose.call(this);
if (this.backupMesh) {
this.backupMesh.geometry = null;
this.backupMesh.materials = null;
this.backupMesh.remove();
this.backupMesh = null;
}
this.flip = 0;
this.orientaiton = 0;
this.isTail = false;
}
R3.CustomCode.SnakeBody.prototype.clone = function() {
return new R3.CustomCode.SnakeBody(
this.type,
this.mesh,
{
x : this.position.x,
y : this.position.y
},
this.orientation,
this.flip,
this.backupMesh,
this.isTail
)
};
R3.CustomCode.SnakeBody.prototype.applyToMesh = function(updateInstance) {
R3.CustomCode.GameObject.prototype.applyToMesh.call(this, updateInstance);
this.mesh.rotation.z = this.orientation * Math.PI / 2;
if (this.backupMesh) {
this.mesh.rotation.z += ((Math.PI / 2) * this.flip);
this.mesh.updateInstance('position');
this.mesh.updateInstance('rotation');
}
this.mesh.updateInstance('rotation');
}
R3.CustomCode.SnakeBody.prototype.advance = function(orientation, flip) {
var crash = false;
switch (orientation) {
case R3.CustomCode.ORIENTATION_UP :
if ((this.position.y + 1) >= R3.CustomCode.GRID_HEIGHT) {
crash = true;
} else {
this.position.y += 1;
}
break;
case R3.CustomCode.ORIENTATION_DOWN :
if ((this.position.y - 1) < 0) {
crash = true;
} else {
this.position.y -= 1;
}
break;
case R3.CustomCode.ORIENTATION_LEFT :
if ((this.position.x - 1) < 0) {
crash = true;
} else {
this.position.x -= 1;
}
break;
case R3.CustomCode.ORIENTATION_RIGHT :
if ((this.position.x + 1) >= R3.CustomCode.GRID_WIDTH) {
crash = true;
} else {
this.position.x += 1;
}
break;
default :
console.warn('unknown orientation');
break;
}
if (crash) {
console.log('crashed!');
}
this.orientation = orientation;
this.flip = flip;
return !crash;
}
R3.CustomCode.prototype.initializeGrid = function() {
this.grid.map(
function(x) {
x.map(
function(y) {
if (y) {
y.dispose();
}
}
);
}
)
this.food = [];
this.powerups = [];
this.snake = [];
this.grid = [];
for (var x = 0; x < R3.CustomCode.GRID_WIDTH; x++) {
this.grid[x] = [];
for (var y = 0; y < R3.CustomCode.GRID_HEIGHT; y++) {
this.grid[x][y] = new R3.CustomCode.GameObject();
}
}
}.bind(this);
/**
* Explode the snake
* @param position
*/
R3.CustomCode.prototype.explode = function(position) {
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Crashed'
}
);
this.state.exploding = true;
this.state.lives -= 1;
this.particleEnginePickle.position.x = (position.x * R3.CustomCode.BODY_SCALE_X) + R3.CustomCode.GRID_OFFSET_X;
this.particleEnginePickle.position.y = (position.y * R3.CustomCode.BODY_SCALE_Y) + R3.CustomCode.GRID_OFFSET_Y;
this.particleEnginePickle.updateInstance('position');
this.particleEnginePickle.enabled = true;
}.bind(this)
/**
* Extend the snake with information from gameObject
* @param gameObject
*/
R3.CustomCode.prototype.extend = function(gameObject, position, orientation) {
if (gameObject.objectType !== R3.CustomCode.OBJECT_TYPE_FOOD) {
throw new Error('wrong type of game object');
}
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Extend'
}
);
var body = null;
switch (gameObject.type) {
case R3.CustomCode.FOOD_BACON :
body = this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_BACON,
{
x : position.x,
y : position.y
},
orientation
)
break;
case R3.CustomCode.FOOD_CHEESE :
body = this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_CHEESE,
{
x : position.x,
y : position.y
},
orientation
)
break;
case R3.CustomCode.FOOD_ONION :
body = this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_ONION,
{
x : position.x,
y : position.y
},
orientation
)
break;
case R3.CustomCode.FOOD_ONION_RING :
body = this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_ONION_RING,
{
x : position.x,
y : position.y
},
orientation
)
break;
case R3.CustomCode.FOOD_PATTY :
body = this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_PATTY,
{
x : position.x,
y : position.y
},
orientation
)
break;
case R3.CustomCode.FOOD_TOMATO :
body = this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_TOMATO,
{
x : position.x,
y : position.y
},
orientation
)
break;
default :
throw new Error('unhandled food type: ' + gameObject.type);
}
/*
if (orientation === R3.CustomCode.ORIENTATION_UP) {
body.mesh.position.y -= 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (orientation === R3.CustomCode.ORIENTATION_DOWN) {
body.mesh.position.y += 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (orientation === R3.CustomCode.ORIENTATION_LEFT) {
body.mesh.position.x += 0.5 * R3.CustomCode.BODY_SCALE_X;
}
if (orientation === R3.CustomCode.ORIENTATION_RIGHT) {
body.mesh.position.x -= 0.5 * R3.CustomCode.BODY_SCALE_X;
}*/
body.mesh.updateInstance('position');
this.state.eating = true;
this.snake.splice(1, 0, body);
/**
* Food or powerup objects need to be destroyed before being replaced with a body object
*/
this.food.splice(
this.food.indexOf(gameObject),
1
);
gameObject.dispose();
this.grid[body.position.x][body.position.y] = body;
if (this.snake.length === R3.CustomCode.GRID_WIDTH * R3.CustomCode.GRID_HEIGHT) {
this.gameOver(true);
}
this.extendSpeedGrain = (R3.CustomCode.SPEED_INITIAL - R3.CustomCode.MAX_SPEED) / ((R3.CustomCode.GRID_WIDTH * R3.CustomCode.GRID_HEIGHT) - this.snake.length)
var animationSpeedGrain = (R3.CustomCode.MAX_ANIMATION_SPEED - R3.CustomCode.MIN_ANIMATION_SPEED) / ((R3.CustomCode.GRID_WIDTH * R3.CustomCode.GRID_HEIGHT) - this.snake.length)
this.speed -= this.extendSpeedGrain;
if (this.speed < R3.CustomCode.MAX_SPEED) {
this.speed = R3.CustomCode.MAX_SPEED;
}
this.animation.translationSpeed += animationSpeedGrain;
if (this.animation.translationSpeed > R3.CustomCode.MAX_ANIMATION_SPEED) {
this.animation.translationSpeed = R3.CustomCode.MAX_ANIMATION_SPEED;
}
this.state.score += Math.round((1 - this.speed) * 100);
this.displayHUD();
//console.log('speed = ' + this.speed);
}.bind(this);
/**
* Powerup the snake with information from gameObject
* @param gameObject
*/
R3.CustomCode.prototype.powerup = function(gameObject) {
if (gameObject.objectType !== R3.CustomCode.OBJECT_TYPE_POWERUP) {
throw new Error('wrong type of game object');
}
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Powerup'
}
);
if (gameObject.type === R3.CustomCode.POWERUP_LIFE) {
this.state.lives += 1;
if (this.state.lives > 3) {
this.state.lives = 3;
}
this.displayHUD();
}
if (gameObject.type === R3.CustomCode.POWERUP_SPEED) {
this.speed -= R3.CustomCode.POWERUP_SPEED_GRAIN;
if (this.speed < R3.CustomCode.MAX_SPEED) {
this.speed = R3.CustomCode.MAX_SPEED;
}
this.animation.translationSpeed += R3.CustomCode.POWERUP_ANIMATION_SPEED_GRAIN;
if (this.animation.translationSpeed > R3.CustomCode.MAX_ANIMATION_SPEED) {
this.animation.translationSpeed = R3.CustomCode.MAX_ANIMATION_SPEED;
}
}
if (gameObject.type === R3.CustomCode.POWERUP_SLOW) {
this.speed += R3.CustomCode.POWERUP_SPEED_GRAIN;
if (this.speed > R3.CustomCode.MIN_SPEED) {
this.speed = R3.CustomCode.MIN_SPEED;
}
this.animation.translationSpeed -= R3.CustomCode.POWERUP_ANIMATION_SPEED_GRAIN;
if (this.animation.translationSpeed < R3.CustomCode.MIN_ANIMATION_SPEED) {
this.animation.translationSpeed = R3.CustomCode.MIN_ANIMATION_SPEED;
}
}
console.log('speed = ' + this.speed);
console.log('animation speed = ' + this.animation.translationSpeed);
this.powerups.splice(
this.powerups.indexOf(gameObject),
1
);
gameObject.dispose();
}.bind(this);
R3.CustomCode.prototype.createCorner = function(body, temp) {
var backupOrientation = body.orientation;
body.orientation = temp.orientation;
body.applyToMesh();
body.backupMesh = body.mesh;
if (temp.orientation === R3.CustomCode.ORIENTATION_UP) {
body.backupMesh.position.y -= 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (temp.orientation === R3.CustomCode.ORIENTATION_DOWN) {
body.backupMesh.position.y += 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (temp.orientation === R3.CustomCode.ORIENTATION_LEFT) {
body.backupMesh.position.x += 0.5 * R3.CustomCode.BODY_SCALE_X;
}
if (temp.orientation === R3.CustomCode.ORIENTATION_RIGHT) {
body.backupMesh.position.x -= 0.5 * R3.CustomCode.BODY_SCALE_X;
}
body.orientation = backupOrientation;
body.mesh = this.createGameMesh(this.materialBreadCorner);
body.mesh.position.z = 5;
body.mesh.updateInstance('position');
body.mesh.visible = true;
body.mesh.updateInstance('visible');
}.bind(this);
R3.CustomCode.prototype.backup = function() {
this.backupState = {
snake : this.snake.map(
function(body) {
return body.clone();
}
),
orientation : this.state.orientation,
flip : this.state.flip
};
}.bind(this)
R3.CustomCode.prototype.restore = function() {
this.snake.map(
function(body) {
/**
* We remove all snake parts from the grid so they are not destroyed on initializeGrid()
* @type {null}
*/
this.grid[body.position.x][body.position.y] = null;
var meshFound = false;
var backupMeshFound = false;
this.backupState.snake.map(
function (backupBody) {
if (
body.mesh === backupBody.mesh ||
body.mesh === backupBody.backupMesh
) {
meshFound = true;
}
if (
body.backupMesh === backupBody.mesh ||
body.backupMesh === backupBody.backupMesh
) {
backupMeshFound = true;
}
}.bind(this)
);
if (!meshFound) {
body.mesh.geometry = null;
body.mesh.materials = null;
body.mesh.remove();
body.mesh = null;
}
if (!backupMeshFound) {
body.backupMesh.geometry = null;
body.backupMesh.materials = null;
body.backupMesh.remove();
body.backupMesh = null;
}
}.bind(this)
)
/**
* Now initialize the grid
*/
this.initializeGrid();
/**
* Restore our backup
*/
this.state.orientation = this.backupState.orientation;
this.state.flip = this.backupState.flip;
this.snake = this.backupState.snake.map(
function(body) {
var clone = body.clone();
if (clone.backupMesh) {
if (clone.mesh) {
clone.mesh.geometry = null;
clone.mesh.materials = null;
clone.mesh.remove();
}
clone.mesh = clone.backupMesh;
clone.backupMesh = null;
clone.mesh.visible = true;
clone.mesh.updateInstance('visible');
clone.applyToMesh();
clone.backupMesh = clone.mesh;
if (clone.orientation === R3.CustomCode.ORIENTATION_UP) {
clone.backupMesh.position.y += 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (clone.orientation === R3.CustomCode.ORIENTATION_DOWN) {
clone.backupMesh.position.y -= 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (clone.orientation === R3.CustomCode.ORIENTATION_LEFT) {
clone.backupMesh.position.x -= 0.5 * R3.CustomCode.BODY_SCALE_X;
}
if (clone.orientation === R3.CustomCode.ORIENTATION_RIGHT) {
clone.backupMesh.position.x += 0.5 * R3.CustomCode.BODY_SCALE_X;
}
if (clone.isTail) {
clone.mesh = clone.backupMesh;
clone.backupMesh = null;
} else {
clone.mesh = this.createGameMesh(this.materialBreadCorner);
}
clone.applyToMesh();
} else {
clone.mesh.visible = true;
clone.mesh.updateInstance('visible');
clone.applyToMesh();
}
this.grid[clone.position.x][clone.position.y] = clone;
return clone;
}.bind(this)
);
this.backup();
this.visualizeGrid();
}.bind(this)
/**
* Move the snake forward and do collision detection
* @type {function(this:snakeEntityLoaded)}
*/
R3.CustomCode.prototype.advanceSnake = function(delta) {
if (this.state.exploding) {
return;
}
this.advanceTime += delta;
if (this.advanceTime > this.speed) {
this.advanceTime = 0;
} else {
return;
}
var backup = null;
var temp = null;
var advanced = false;
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Move'
}
);
this.snake.map(
function(body, index) {
if (index === 0) {
if (body.backupMesh) {
/**
* We used to be a corner, change back
* @type {null}
*/
body.mesh.geometry = null;
body.mesh.materials = null;
body.mesh.remove();
body.mesh = body.backupMesh;
body.backupMesh = null;
body.mesh.visible = true;
body.mesh.updateInstance('visible');
body.mesh.updateInstance('position');
}
backup = {
position : {
x : body.position.x,
y : body.position.y
},
orientation : body.orientation,
flip : body.flip
}
advanced = body.advance(
this.state.orientation,
this.state.flip,
this.grid
);
this.grid[backup.position.x][backup.position.y] = new R3.CustomCode.GameObject();
if (!advanced) {
this.explode(body.position);
} else {
var gameObject = this.grid[body.position.x][body.position.y];
switch (gameObject.objectType) {
case R3.CustomCode.OBJECT_TYPE_SNAKE_BODY :
if (!gameObject.isTail) {
if (body.orientation !== backup.orientation) {
/**
* This head is bent
*/
body.applyToMesh();
body.backupMesh = body.mesh;
if (body.orientation === R3.CustomCode.ORIENTATION_UP) {
body.backupMesh.position.y -= 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (body.orientation === R3.CustomCode.ORIENTATION_DOWN) {
body.backupMesh.position.y += 0.5 * R3.CustomCode.BODY_SCALE_Y;
}
if (body.orientation === R3.CustomCode.ORIENTATION_LEFT) {
body.backupMesh.position.x += 0.5 * R3.CustomCode.BODY_SCALE_X;
}
if (body.orientation === R3.CustomCode.ORIENTATION_RIGHT) {
body.backupMesh.position.x -= 0.5 * R3.CustomCode.BODY_SCALE_X;
}
body.mesh = this.createGameMesh(this.materialBreadCorner);
var tempPos = {
x : body.position.x,
y : body.position.y
}
body.position.x = backup.position.x;
body.position.y = backup.position.y;
body.mesh.position.z = 5;
body.mesh.updateInstance('position');
body.mesh.visible = true;
body.mesh.updateInstance('visible');
body.applyToMesh();
this.explode(tempPos);
} else {
this.explode(body.position);
}
}
break;
case R3.CustomCode.OBJECT_TYPE_FOOD :
this.extend(gameObject, backup.position, backup.orientation);
break;
case R3.CustomCode.OBJECT_TYPE_POWERUP:
this.powerup(gameObject);
break;
default:
break;
}
}
this.grid[body.position.x][body.position.y] = body
this.visualizeGrid();
backup.orientation = body.orientation;
backup.flip = body.flip;
} else {
if (this.state.exploding === true) {
return;
}
temp = {
position : {
x : body.position.x,
y : body.position.y
},
orientation : body.orientation,
flip : body.flip
}
if (this.state.eating === true) {
if ((body.orientation !== backup.orientation) &&
(index === 1)) {
body.orientation = backup.orientation;
body.flip = backup.flip;
this.createCorner(body, temp);
body.applyToMesh();
}
return;
}
/**
* Create a new empty object at the current grid position (free it up)
*/
if (this.grid[body.position.x][body.position.y] === this.snake[0]) {
/**
* Its possible that we just nearly missed the tail with the head - and avoid a
* collision - in this case - don't reset this position since it belongs to the head
*/
} else {
this.grid[body.position.x][body.position.y] = new R3.CustomCode.GameObject();
}
/**
* Assign the new location to the body
*/
body.position.x = backup.position.x;
body.position.y = backup.position.y;
body.orientation = backup.orientation;
body.flip = backup.flip;
/**
* Update the grid with our new body
*/
this.grid[body.position.x][body.position.y] = body;
if (body.backupMesh) {
/**
* We used to be a corner, change back
* @type {null}
*/
body.mesh.geometry = null;
body.mesh.materials = null;
body.mesh.remove();
body.mesh = body.backupMesh;
body.backupMesh = null;
body.mesh.visible = true;
body.mesh.updateInstance('visible');
body.mesh.updateInstance('position');
}
if (body.orientation !== temp.orientation) {
this.createCorner(body, temp);
}
backup = temp;
}
body.applyToMesh();
}.bind(this)
)
this.state.turning = false;
if (this.state.eating) {
this.backup();
}
this.state.eating = false;
this.visualizeGrid();
}.bind(this);
R3.Event.Subscribe(
R3.Event.GAME_START,
function() {
this.state = {
orientation : 0,
turning : false,
flip : 0,
eating : false,
exploding : false,
lives : 3,
getReady : false,
message : null,
gameOver : false,
muted : this.state.muted,
paused : false,
score : 0,
highscore : this.state.highscore
};
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Techno BG Music'
}
);
this.initializeGrid();
this.visualizeGrid();
this.displayHUD();
this.snake = [
this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_HEAD,
{
x : 3,
y : 2
},
R3.CustomCode.ORIENTATION_UP
),
this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_PATTY,
{
x : 3,
y : 1
},
R3.CustomCode.ORIENTATION_UP
),
this.createGameObject(
R3.CustomCode.OBJECT_TYPE_SNAKE_BODY,
R3.CustomCode.BODY_TYPE_BREAD_TAIL,
{
x : 3,
y : 0
},
R3.CustomCode.ORIENTATION_UP
)
]
this.backup();
/**
* Other Settings
*/
this.advanceTime = 0;
this.speed = R3.CustomCode.SPEED_INITIAL;
this.foodTime = 0;
this.foodSpeed = R3.CustomCode.FOOD_SPEED_INITIAL;
this.powerupTime = 0;
this.nextPowerupTime = R3.Utils.GetRandomIntInclusive(
R3.CustomCode.POWERUP_WAIT_TIME_MIN,
R3.CustomCode.POWERUP_WAIT_TIME_MAX
);
this.explodeTime = 0;
this.speedTimer = 0;
R3.Event.Emit(
R3.Event.GAME_DATA,
{
type : 'game_start',
state : this.state
}
);
/**
* Re-initialize our other custom code components
*/
this.beforeRender.initialized = false;
this.beforeRender.entityLoaded = this;
this.keyUp.initialized = false;
this.keyUp.entityLoaded = this;
this.touchEnd.initialized = false;
this.touchEnd.entityLoaded = this;
this.mouseDown.initialized = false;
this.mouseDown.entityLoaded = this;
this.mouseUp.initialized = false;
this.mouseUp.entityLoaded = this;
this.touchStart.initialized = false;
this.touchStart.entityLoaded = this;
console.log('starting game snake');
}.bind(this)
);
R3.Event.Subscribe(
R3.Event.GAME_OVER,
function() {
this.keyUp.initialized = false;
this.keyUp.entityLoaded = null;
this.beforeRender.initialized = false;
this.beforeRender.entityLoaded = null;
this.touchEnd.initialized = false;
this.touchEnd.entityLoaded = null;
this.mouseDown.initialized = false;
this.mouseDown.entityLoaded = null;
this.mouseUp.initialized = false;
this.mouseUp.entityLoaded = null;
this.touchStart.initialized = false;
this.touchStart.entityLoaded = null;
R3.Event.Emit(
R3.Event.GAME_DATA,
{
type : 'game_over',
state : this.state
}
);
console.log('game over');
}.bind(this)
)
R3.Event.Subscribe(
R3.Event.ENGINE_FIRED_PARTICLES_ZERO,
function(data) {
if (data.component === this.particleEnginePickle) {
this.particleEnginePickle.enabled = false;
this.particleEnginePickle.fired = false;
}
}.bind(this)
)
R3.Event.Subscribe(
R3.Event.GAME_DATA,
function(data) {
if (data.type === 'highscore') {
this.state.highscore = data.highscore;
this.displayHUD();
}
}.bind(this)
)
this.displayHUD();
R3.Event.Emit(R3.Event.GAME_LOADED);
//@ sourceURL=entityLoaded.js