Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 3581 bytes modified
parent
1d555ad20b
commit
2b325df5d1
243
21g30t1e75.js
243
21g30t1e75.js
|
@ -1,68 +1,78 @@
|
|||
if (this.parentEntity === data.entity) {
|
||||
console.log('snake entity loaded');
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
function snakeEntityLoaded(data) {
|
||||
|
||||
/**
|
||||
if (this.parentEntity === data.entity) {
|
||||
console.log('snake entity loaded');
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines
|
||||
*/
|
||||
GameLib.CustomCode.BODY_TYPE_NORMAL = 0x1;
|
||||
GameLib.CustomCode.BODY_TYPE_TAIL = 0x2;
|
||||
GameLib.CustomCode.BODY_TYPE_NORMAL = 0x1;
|
||||
GameLib.CustomCode.BODY_TYPE_TAIL = 0x2;
|
||||
|
||||
GameLib.CustomCode.GRID_WIDTH = 11;
|
||||
GameLib.CustomCode.GRID_HEIGHT = 11;
|
||||
GameLib.CustomCode.GRID_WIDTH = 11;
|
||||
GameLib.CustomCode.GRID_HEIGHT = 11;
|
||||
|
||||
GameLib.CustomCode.GRID_OFFSET_X = 1;
|
||||
GameLib.CustomCode.GRID_OFFSET_Y = 5;
|
||||
GameLib.CustomCode.GRID_OFFSET_X = 1;
|
||||
GameLib.CustomCode.GRID_OFFSET_Y = 5;
|
||||
|
||||
GameLib.CustomCode.SPEED_INITIAL = 1;
|
||||
GameLib.CustomCode.SPEED_INITIAL = 1;
|
||||
|
||||
/**
|
||||
GameLib.CustomCode.ORIENTATION_UP = 0;
|
||||
GameLib.CustomCode.ORIENTATION_LEFT = 1;
|
||||
GameLib.CustomCode.ORIENTATION_DOWN = 2;
|
||||
GameLib.CustomCode.ORIENTATION_RIGHT = 3;
|
||||
|
||||
/**
|
||||
* Get runtime
|
||||
*/
|
||||
this.runtime = GameLib.Utils.GetRuntime();
|
||||
this.runtime = GameLib.Utils.GetRuntime();
|
||||
|
||||
/**
|
||||
/**
|
||||
* Custom Code Components
|
||||
*/
|
||||
this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt');
|
||||
this.keyUp = GameLib.EntityManager.Instance.findComponentById('306204wy29');
|
||||
this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt');
|
||||
this.keyUp = GameLib.EntityManager.Instance.findComponentById('306204wy29');
|
||||
|
||||
/**
|
||||
/**
|
||||
* Geometries
|
||||
*/
|
||||
this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp');
|
||||
this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp');
|
||||
|
||||
/**
|
||||
/**
|
||||
* Images
|
||||
*/
|
||||
this.imageBreadEnd = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
|
||||
this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
|
||||
this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok');
|
||||
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
|
||||
this.imageBreadHead = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
|
||||
this.imageBreadTail = GameLib.EntityManager.Instance.findComponentById('se6qlnmojd');
|
||||
this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
|
||||
this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok');
|
||||
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
|
||||
|
||||
/**
|
||||
/**
|
||||
* Other Objects (Scene)
|
||||
*/
|
||||
this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj');
|
||||
this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj');
|
||||
|
||||
/**
|
||||
/**
|
||||
* Game objects
|
||||
*/
|
||||
this.snake = [];
|
||||
this.grid = [];
|
||||
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
||||
this.advanceTime = 0;
|
||||
this.state = {
|
||||
direction : {
|
||||
x : 0,
|
||||
y : 1
|
||||
},
|
||||
rotation : 0
|
||||
};
|
||||
this.snake = [];
|
||||
this.grid = [];
|
||||
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
||||
this.advanceTime = 0;
|
||||
|
||||
GameLib.CustomCode.prototype.createMaterial = function(image) {
|
||||
/**
|
||||
* 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
|
||||
};
|
||||
|
||||
GameLib.CustomCode.prototype.createMaterial = function(image) {
|
||||
var diffuseMap = new GameLib.D3.Texture.Image(
|
||||
this.runtime.graphics,
|
||||
{
|
||||
|
@ -76,9 +86,9 @@ GameLib.CustomCode.prototype.createMaterial = function(image) {
|
|||
diffuseMap : diffuseMap
|
||||
}
|
||||
);
|
||||
}.bind(this);
|
||||
}.bind(this);
|
||||
|
||||
GameLib.CustomCode.prototype.createGameMesh = function(image) {
|
||||
GameLib.CustomCode.prototype.createGameMesh = function(image) {
|
||||
|
||||
var mesh = new GameLib.D3.Mesh(
|
||||
this.runtime.graphics,
|
||||
|
@ -92,26 +102,39 @@ GameLib.CustomCode.prototype.createGameMesh = function(image) {
|
|||
|
||||
return mesh;
|
||||
|
||||
}.bind(this)
|
||||
}.bind(this)
|
||||
|
||||
/**
|
||||
/**
|
||||
* Create our objects
|
||||
*/
|
||||
this.meshBreadEnd = this.createGameMesh(this.imageBreadEnd);
|
||||
this.meshBreadEnd.useQuaternion = false;
|
||||
this.meshBreadHead = this.createGameMesh(this.imageBreadHead);
|
||||
this.meshBreadHead.useQuaternion = false;
|
||||
|
||||
this.meshPatty = this.createGameMesh(this.imagePatty);
|
||||
this.meshBreadTail = this.createGameMesh(this.imageBreadTail);
|
||||
this.meshBreadTail.useQuaternion = false;
|
||||
|
||||
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty);
|
||||
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
|
||||
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty);
|
||||
this.meshBreadPatty.useQuaternion = false;
|
||||
|
||||
GameLib.CustomCode.SnakeBody = function(
|
||||
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
|
||||
this.meshBreadCorner.useQuaternion = false;
|
||||
|
||||
this.meshPatty = this.createGameMesh(this.imagePatty);
|
||||
|
||||
/**
|
||||
* GameLib.CustomCode.SnakeBody
|
||||
* @param mesh
|
||||
* @param position
|
||||
* @param orientation
|
||||
* @constructor
|
||||
*/
|
||||
GameLib.CustomCode.SnakeBody = function(
|
||||
mesh,
|
||||
position,
|
||||
rotation
|
||||
) {
|
||||
orientation
|
||||
) {
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
||||
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
||||
throw new Error('no mesh specified');
|
||||
}
|
||||
this.mesh = mesh.clone();
|
||||
|
@ -124,36 +147,50 @@ if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
|||
}
|
||||
this.position = position;
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(rotation)) {
|
||||
rotation = 0;
|
||||
if (GameLib.Utils.UndefinedOrNull(orientation)) {
|
||||
orientation = GameLib.CustomCode.ORIENTATION_UP;
|
||||
}
|
||||
this.rotation = rotation;
|
||||
this.orientation = orientation;
|
||||
|
||||
this.applyToMesh();
|
||||
};
|
||||
};
|
||||
|
||||
GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
|
||||
GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
|
||||
|
||||
this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X;
|
||||
this.mesh.position.y = this.position.y + GameLib.CustomCode.GRID_OFFSET_Y;
|
||||
|
||||
this.mesh.rotation.z = this.rotation;
|
||||
this.mesh.rotation.z = this.orientation * Math.PI;
|
||||
|
||||
/**
|
||||
* TODO: We don't update instance position - animation should do this
|
||||
*/
|
||||
this.mesh.updateInstance('position');
|
||||
this.mesh.updateInstance('rotation');
|
||||
}
|
||||
}
|
||||
|
||||
GameLib.CustomCode.SnakeBody.prototype.advance = function(direction, rotation) {
|
||||
this.position.x += direction.x;
|
||||
this.position.y += direction.y;
|
||||
GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation) {
|
||||
|
||||
this.rotation = rotation;
|
||||
}
|
||||
if (orientation === GameLib.CustomCode.ORIENTATION_UP) {
|
||||
this.position.y += 1;
|
||||
}
|
||||
|
||||
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
||||
if (orientation === GameLib.CustomCode.ORIENTATION_DOWN) {
|
||||
this.position.y -= 1;
|
||||
}
|
||||
|
||||
if (orientation === GameLib.CustomCode.ORIENTATION_LEFT) {
|
||||
this.position.x -= 1;
|
||||
}
|
||||
|
||||
if (orientation === GameLib.CustomCode.ORIENTATION_RIGHT) {
|
||||
this.position.x += 1;
|
||||
}
|
||||
|
||||
this.orientation = orientation;
|
||||
}
|
||||
|
||||
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
||||
|
||||
this.advanceTime += delta;
|
||||
|
||||
|
@ -163,55 +200,50 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
|||
return;
|
||||
}
|
||||
|
||||
var position = {x:0, y:0};
|
||||
//var rotation = 0;
|
||||
var backup = {
|
||||
x : 0,
|
||||
y : 0,
|
||||
orientation : GameLib.CustomCode.ORIENTATION_UP
|
||||
};
|
||||
|
||||
this.snake.map(
|
||||
function(body, index) {
|
||||
|
||||
if (index === 0) {
|
||||
|
||||
position.x = body.position.x;
|
||||
position.y = body.position.y;
|
||||
|
||||
//rotation = body.rotation;
|
||||
backup.x = body.position.x;
|
||||
backup.y = body.position.y;
|
||||
backup.orientation = body.orientation;
|
||||
|
||||
body.advance(
|
||||
this.state.direction,
|
||||
this.state.rotation
|
||||
this.state.orientation
|
||||
);
|
||||
|
||||
} else {
|
||||
|
||||
var tempPosition = {
|
||||
var temp = {
|
||||
x : body.position.x,
|
||||
y : body.position.y
|
||||
y : body.position.y,
|
||||
orientation : body.orientation
|
||||
};
|
||||
|
||||
//var tempRotation = body.rotation;
|
||||
|
||||
body.position.x = position.x;
|
||||
body.position.y = position.y;
|
||||
|
||||
//body.rotation += this.state.rotation;
|
||||
|
||||
position.x = tempPosition.x;
|
||||
position.y = tempPosition.y;
|
||||
|
||||
//rotation = tempRotation;
|
||||
body.position.x = backup.x;
|
||||
body.position.y = backup.y;
|
||||
body.position.orientation = backup.orientation;
|
||||
|
||||
backup.x = temp.x;
|
||||
backup.y = temp.y;
|
||||
backup.orientation = temp.orientation;
|
||||
}
|
||||
|
||||
body.applyToMesh();
|
||||
}.bind(this)
|
||||
);
|
||||
|
||||
this.state.rotation = 0;
|
||||
}.bind(this);
|
||||
|
||||
}.bind(this);
|
||||
|
||||
/*
|
||||
GameLib.CustomCode.prototype.initializeGrid = function() {
|
||||
/*
|
||||
GameLib.CustomCode.prototype.initializeGrid = function() {
|
||||
|
||||
this.grid = [];
|
||||
|
||||
|
@ -228,10 +260,10 @@ GameLib.CustomCode.prototype.initializeGrid = function() {
|
|||
}
|
||||
}
|
||||
|
||||
}.bind(this);
|
||||
*/
|
||||
}.bind(this);
|
||||
*/
|
||||
|
||||
GameLib.Event.Subscribe(
|
||||
GameLib.Event.Subscribe(
|
||||
GameLib.Event.GAME_START,
|
||||
function() {
|
||||
|
||||
|
@ -247,16 +279,12 @@ GameLib.Event.Subscribe(
|
|||
)
|
||||
|
||||
this.state = {
|
||||
direction : {
|
||||
x : 0,
|
||||
y : 1
|
||||
},
|
||||
rotation : 0
|
||||
orientation : GameLib.CustomCode.ORIENTATION_UP
|
||||
};
|
||||
|
||||
this.snake = [
|
||||
new GameLib.CustomCode.SnakeBody(
|
||||
this.meshBreadEnd,
|
||||
this.meshBreadHead,
|
||||
{
|
||||
x : 4,
|
||||
y : 4
|
||||
|
@ -280,12 +308,12 @@ GameLib.Event.Subscribe(
|
|||
0
|
||||
),
|
||||
new GameLib.CustomCode.SnakeBody(
|
||||
this.meshBreadEnd,
|
||||
this.meshBreadTail,
|
||||
{
|
||||
x : 4,
|
||||
y : 1
|
||||
},
|
||||
Math.PI
|
||||
0
|
||||
)
|
||||
];
|
||||
|
||||
|
@ -323,9 +351,9 @@ GameLib.Event.Subscribe(
|
|||
|
||||
console.log('starting game snake');
|
||||
}.bind(this)
|
||||
);
|
||||
);
|
||||
|
||||
GameLib.Event.Subscribe(
|
||||
GameLib.Event.Subscribe(
|
||||
GameLib.Event.GAME_OVER,
|
||||
function() {
|
||||
|
||||
|
@ -337,8 +365,9 @@ GameLib.Event.Subscribe(
|
|||
|
||||
console.log('starting game snake');
|
||||
}.bind(this)
|
||||
)
|
||||
)
|
||||
|
||||
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
||||
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
||||
|
||||
//@ sourceURL=entityLoaded.js
|
||||
}
|
Loading…
Reference in New Issue