Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 3581 bytes modified
parent
1d555ad20b
commit
2b325df5d1
131
21g30t1e75.js
131
21g30t1e75.js
|
@ -1,3 +1,5 @@
|
||||||
|
function snakeEntityLoaded(data) {
|
||||||
|
|
||||||
if (this.parentEntity === data.entity) {
|
if (this.parentEntity === data.entity) {
|
||||||
console.log('snake entity loaded');
|
console.log('snake entity loaded');
|
||||||
} else {
|
} else {
|
||||||
|
@ -18,6 +20,11 @@ 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
|
* Get runtime
|
||||||
*/
|
*/
|
||||||
|
@ -37,7 +44,8 @@ this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp
|
||||||
/**
|
/**
|
||||||
* Images
|
* Images
|
||||||
*/
|
*/
|
||||||
this.imageBreadEnd = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
|
this.imageBreadHead = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
|
||||||
|
this.imageBreadTail = GameLib.EntityManager.Instance.findComponentById('se6qlnmojd');
|
||||||
this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
|
this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
|
||||||
this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok');
|
this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok');
|
||||||
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
|
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
|
||||||
|
@ -54,12 +62,14 @@ this.snake = [];
|
||||||
this.grid = [];
|
this.grid = [];
|
||||||
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
||||||
this.advanceTime = 0;
|
this.advanceTime = 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 = {
|
this.state = {
|
||||||
direction : {
|
orientation : 0
|
||||||
x : 0,
|
|
||||||
y : 1
|
|
||||||
},
|
|
||||||
rotation : 0
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.CustomCode.prototype.createMaterial = function(image) {
|
GameLib.CustomCode.prototype.createMaterial = function(image) {
|
||||||
|
@ -97,18 +107,31 @@ GameLib.CustomCode.prototype.createGameMesh = function(image) {
|
||||||
/**
|
/**
|
||||||
* Create our objects
|
* Create our objects
|
||||||
*/
|
*/
|
||||||
this.meshBreadEnd = this.createGameMesh(this.imageBreadEnd);
|
this.meshBreadHead = this.createGameMesh(this.imageBreadHead);
|
||||||
this.meshBreadEnd.useQuaternion = false;
|
this.meshBreadHead.useQuaternion = false;
|
||||||
|
|
||||||
|
this.meshBreadTail = this.createGameMesh(this.imageBreadTail);
|
||||||
|
this.meshBreadTail.useQuaternion = false;
|
||||||
|
|
||||||
|
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty);
|
||||||
|
this.meshBreadPatty.useQuaternion = false;
|
||||||
|
|
||||||
|
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
|
||||||
|
this.meshBreadCorner.useQuaternion = false;
|
||||||
|
|
||||||
this.meshPatty = this.createGameMesh(this.imagePatty);
|
this.meshPatty = this.createGameMesh(this.imagePatty);
|
||||||
|
|
||||||
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty);
|
/**
|
||||||
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
|
* GameLib.CustomCode.SnakeBody
|
||||||
|
* @param mesh
|
||||||
|
* @param position
|
||||||
|
* @param orientation
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
GameLib.CustomCode.SnakeBody = function(
|
GameLib.CustomCode.SnakeBody = function(
|
||||||
mesh,
|
mesh,
|
||||||
position,
|
position,
|
||||||
rotation
|
orientation
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
||||||
|
@ -124,10 +147,10 @@ if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
||||||
}
|
}
|
||||||
this.position = position;
|
this.position = position;
|
||||||
|
|
||||||
if (GameLib.Utils.UndefinedOrNull(rotation)) {
|
if (GameLib.Utils.UndefinedOrNull(orientation)) {
|
||||||
rotation = 0;
|
orientation = GameLib.CustomCode.ORIENTATION_UP;
|
||||||
}
|
}
|
||||||
this.rotation = rotation;
|
this.orientation = orientation;
|
||||||
|
|
||||||
this.applyToMesh();
|
this.applyToMesh();
|
||||||
};
|
};
|
||||||
|
@ -137,7 +160,7 @@ GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
|
||||||
this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X;
|
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.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
|
* TODO: We don't update instance position - animation should do this
|
||||||
|
@ -146,11 +169,25 @@ GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
|
||||||
this.mesh.updateInstance('rotation');
|
this.mesh.updateInstance('rotation');
|
||||||
}
|
}
|
||||||
|
|
||||||
GameLib.CustomCode.SnakeBody.prototype.advance = function(direction, rotation) {
|
GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation) {
|
||||||
this.position.x += direction.x;
|
|
||||||
this.position.y += direction.y;
|
|
||||||
|
|
||||||
this.rotation = rotation;
|
if (orientation === GameLib.CustomCode.ORIENTATION_UP) {
|
||||||
|
this.position.y += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
||||||
|
@ -163,51 +200,46 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var position = {x:0, y:0};
|
var backup = {
|
||||||
//var rotation = 0;
|
x : 0,
|
||||||
|
y : 0,
|
||||||
|
orientation : GameLib.CustomCode.ORIENTATION_UP
|
||||||
|
};
|
||||||
|
|
||||||
this.snake.map(
|
this.snake.map(
|
||||||
function(body, index) {
|
function(body, index) {
|
||||||
|
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
|
|
||||||
position.x = body.position.x;
|
backup.x = body.position.x;
|
||||||
position.y = body.position.y;
|
backup.y = body.position.y;
|
||||||
|
backup.orientation = body.orientation;
|
||||||
//rotation = body.rotation;
|
|
||||||
|
|
||||||
body.advance(
|
body.advance(
|
||||||
this.state.direction,
|
this.state.orientation
|
||||||
this.state.rotation
|
|
||||||
);
|
);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var tempPosition = {
|
var temp = {
|
||||||
x : body.position.x,
|
x : body.position.x,
|
||||||
y : body.position.y
|
y : body.position.y,
|
||||||
|
orientation : body.orientation
|
||||||
};
|
};
|
||||||
|
|
||||||
//var tempRotation = body.rotation;
|
body.position.x = backup.x;
|
||||||
|
body.position.y = backup.y;
|
||||||
body.position.x = position.x;
|
body.position.orientation = backup.orientation;
|
||||||
body.position.y = position.y;
|
|
||||||
|
|
||||||
//body.rotation += this.state.rotation;
|
|
||||||
|
|
||||||
position.x = tempPosition.x;
|
|
||||||
position.y = tempPosition.y;
|
|
||||||
|
|
||||||
//rotation = tempRotation;
|
|
||||||
|
|
||||||
|
backup.x = temp.x;
|
||||||
|
backup.y = temp.y;
|
||||||
|
backup.orientation = temp.orientation;
|
||||||
}
|
}
|
||||||
|
|
||||||
body.applyToMesh();
|
body.applyToMesh();
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
this.state.rotation = 0;
|
|
||||||
|
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -247,16 +279,12 @@ GameLib.Event.Subscribe(
|
||||||
)
|
)
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
direction : {
|
orientation : GameLib.CustomCode.ORIENTATION_UP
|
||||||
x : 0,
|
|
||||||
y : 1
|
|
||||||
},
|
|
||||||
rotation : 0
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.snake = [
|
this.snake = [
|
||||||
new GameLib.CustomCode.SnakeBody(
|
new GameLib.CustomCode.SnakeBody(
|
||||||
this.meshBreadEnd,
|
this.meshBreadHead,
|
||||||
{
|
{
|
||||||
x : 4,
|
x : 4,
|
||||||
y : 4
|
y : 4
|
||||||
|
@ -280,12 +308,12 @@ GameLib.Event.Subscribe(
|
||||||
0
|
0
|
||||||
),
|
),
|
||||||
new GameLib.CustomCode.SnakeBody(
|
new GameLib.CustomCode.SnakeBody(
|
||||||
this.meshBreadEnd,
|
this.meshBreadTail,
|
||||||
{
|
{
|
||||||
x : 4,
|
x : 4,
|
||||||
y : 1
|
y : 1
|
||||||
},
|
},
|
||||||
Math.PI
|
0
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -342,3 +370,4 @@ GameLib.Event.Subscribe(
|
||||||
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
||||||
|
|
||||||
//@ sourceURL=entityLoaded.js
|
//@ sourceURL=entityLoaded.js
|
||||||
|
}
|
Loading…
Reference in New Issue