Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 3581 bytes modified

beta.r3js.org
-=yb4f310 2018-03-14 11:53:15 +01:00
parent 1d555ad20b
commit 2b325df5d1
1 changed files with 335 additions and 306 deletions

View File

@ -1,299 +1,327 @@
if (this.parentEntity === data.entity) { function snakeEntityLoaded(data) {
console.log('snake entity loaded');
} else {
return;
}
/** if (this.parentEntity === data.entity) {
* Defines console.log('snake entity loaded');
*/ } else {
GameLib.CustomCode.BODY_TYPE_NORMAL = 0x1; return;
GameLib.CustomCode.BODY_TYPE_TAIL = 0x2; }
GameLib.CustomCode.GRID_WIDTH = 11; /**
GameLib.CustomCode.GRID_HEIGHT = 11; * Defines
*/
GameLib.CustomCode.BODY_TYPE_NORMAL = 0x1;
GameLib.CustomCode.BODY_TYPE_TAIL = 0x2;
GameLib.CustomCode.GRID_OFFSET_X = 1; GameLib.CustomCode.GRID_WIDTH = 11;
GameLib.CustomCode.GRID_OFFSET_Y = 5; GameLib.CustomCode.GRID_HEIGHT = 11;
GameLib.CustomCode.SPEED_INITIAL = 1; GameLib.CustomCode.GRID_OFFSET_X = 1;
GameLib.CustomCode.GRID_OFFSET_Y = 5;
/** GameLib.CustomCode.SPEED_INITIAL = 1;
* Get runtime
*/
this.runtime = GameLib.Utils.GetRuntime();
/** GameLib.CustomCode.ORIENTATION_UP = 0;
* Custom Code Components GameLib.CustomCode.ORIENTATION_LEFT = 1;
*/ GameLib.CustomCode.ORIENTATION_DOWN = 2;
this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt'); GameLib.CustomCode.ORIENTATION_RIGHT = 3;
this.keyUp = GameLib.EntityManager.Instance.findComponentById('306204wy29');
/** /**
* Geometries * Get runtime
*/ */
this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp'); this.runtime = GameLib.Utils.GetRuntime();
/** /**
* Images * Custom Code Components
*/ */
this.imageBreadEnd = GameLib.EntityManager.Instance.findComponentById('swkla3wpp'); this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt');
this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr'); this.keyUp = GameLib.EntityManager.Instance.findComponentById('306204wy29');
this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok');
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
/** /**
* Other Objects (Scene) * Geometries
*/ */
this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj'); this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp');
/** /**
* Game objects * Images
*/ */
this.snake = []; this.imageBreadHead = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
this.grid = []; this.imageBreadTail = GameLib.EntityManager.Instance.findComponentById('se6qlnmojd');
this.speed = GameLib.CustomCode.SPEED_INITIAL; this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
this.advanceTime = 0; this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok');
this.state = { this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
direction : {
x : 0,
y : 1
},
rotation : 0
};
GameLib.CustomCode.prototype.createMaterial = function(image) { /**
var diffuseMap = new GameLib.D3.Texture.Image( * Other Objects (Scene)
this.runtime.graphics, */
{ this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj');
image : image
}
)
return new GameLib.D3.Material.Phong( /**
this.runtime.graphics, * Game objects
{ */
diffuseMap : diffuseMap this.snake = [];
} this.grid = [];
); this.speed = GameLib.CustomCode.SPEED_INITIAL;
}.bind(this); this.advanceTime = 0;
GameLib.CustomCode.prototype.createGameMesh = 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
};
var mesh = new GameLib.D3.Mesh( GameLib.CustomCode.prototype.createMaterial = function(image) {
this.runtime.graphics, var diffuseMap = new GameLib.D3.Texture.Image(
{ this.runtime.graphics,
geometry : this.geometryBody, {
materials : [this.createMaterial(image)] image : image
} }
) )
this.scene.addClone(mesh); return new GameLib.D3.Material.Phong(
this.runtime.graphics,
{
diffuseMap : diffuseMap
}
);
}.bind(this);
return mesh; GameLib.CustomCode.prototype.createGameMesh = function(image) {
}.bind(this) var mesh = new GameLib.D3.Mesh(
this.runtime.graphics,
{
geometry : this.geometryBody,
materials : [this.createMaterial(image)]
}
)
/** this.scene.addClone(mesh);
* Create our objects
*/
this.meshBreadEnd = this.createGameMesh(this.imageBreadEnd);
this.meshBreadEnd.useQuaternion = false;
this.meshPatty = this.createGameMesh(this.imagePatty); return mesh;
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty); }.bind(this)
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
GameLib.CustomCode.SnakeBody = function( /**
mesh, * Create our objects
position, */
rotation this.meshBreadHead = this.createGameMesh(this.imageBreadHead);
) { this.meshBreadHead.useQuaternion = false;
if (GameLib.Utils.UndefinedOrNull(mesh)) { this.meshBreadTail = this.createGameMesh(this.imageBreadTail);
throw new Error('no mesh specified'); this.meshBreadTail.useQuaternion = false;
}
this.mesh = mesh.clone();
if (GameLib.Utils.UndefinedOrNull(position)) { this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty);
position = { this.meshBreadPatty.useQuaternion = false;
x : Math.round(GameLib.CustomCode.GRID_WIDTH / 2),
y : Math.round(GameLib.CustomCode.GRID_HEIGHT / 2)
};
}
this.position = position;
if (GameLib.Utils.UndefinedOrNull(rotation)) { this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
rotation = 0; this.meshBreadCorner.useQuaternion = false;
}
this.rotation = rotation;
this.applyToMesh(); this.meshPatty = this.createGameMesh(this.imagePatty);
};
GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() { /**
* GameLib.CustomCode.SnakeBody
* @param mesh
* @param position
* @param orientation
* @constructor
*/
GameLib.CustomCode.SnakeBody = function(
mesh,
position,
orientation
) {
this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X; if (GameLib.Utils.UndefinedOrNull(mesh)) {
this.mesh.position.y = this.position.y + GameLib.CustomCode.GRID_OFFSET_Y; throw new Error('no mesh specified');
}
this.mesh = mesh.clone();
this.mesh.rotation.z = this.rotation; if (GameLib.Utils.UndefinedOrNull(position)) {
position = {
x : Math.round(GameLib.CustomCode.GRID_WIDTH / 2),
y : Math.round(GameLib.CustomCode.GRID_HEIGHT / 2)
};
}
this.position = position;
/** if (GameLib.Utils.UndefinedOrNull(orientation)) {
* TODO: We don't update instance position - animation should do this orientation = GameLib.CustomCode.ORIENTATION_UP;
*/ }
this.mesh.updateInstance('position'); this.orientation = orientation;
this.mesh.updateInstance('rotation');
}
GameLib.CustomCode.SnakeBody.prototype.advance = function(direction, rotation) { this.applyToMesh();
this.position.x += direction.x; };
this.position.y += direction.y;
this.rotation = rotation; GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
}
GameLib.CustomCode.prototype.advanceSnake = function(delta) { 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.advanceTime += delta; this.mesh.rotation.z = this.orientation * Math.PI;
if (this.advanceTime > this.speed) { /**
this.advanceTime = 0; * TODO: We don't update instance position - animation should do this
} else { */
return; this.mesh.updateInstance('position');
} this.mesh.updateInstance('rotation');
}
var position = {x:0, y:0}; GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation) {
//var rotation = 0;
this.snake.map( if (orientation === GameLib.CustomCode.ORIENTATION_UP) {
function(body, index) { this.position.y += 1;
}
if (index === 0) { if (orientation === GameLib.CustomCode.ORIENTATION_DOWN) {
this.position.y -= 1;
}
position.x = body.position.x; if (orientation === GameLib.CustomCode.ORIENTATION_LEFT) {
position.y = body.position.y; this.position.x -= 1;
}
//rotation = body.rotation; if (orientation === GameLib.CustomCode.ORIENTATION_RIGHT) {
this.position.x += 1;
}
body.advance( this.orientation = orientation;
this.state.direction, }
this.state.rotation
);
} else { GameLib.CustomCode.prototype.advanceSnake = function(delta) {
var tempPosition = { this.advanceTime += delta;
x : body.position.x,
y : body.position.y
};
//var tempRotation = body.rotation; if (this.advanceTime > this.speed) {
this.advanceTime = 0;
} else {
return;
}
body.position.x = position.x; var backup = {
body.position.y = position.y; x : 0,
y : 0,
orientation : GameLib.CustomCode.ORIENTATION_UP
};
//body.rotation += this.state.rotation; this.snake.map(
function(body, index) {
position.x = tempPosition.x; if (index === 0) {
position.y = tempPosition.y;
//rotation = tempRotation; backup.x = body.position.x;
backup.y = body.position.y;
backup.orientation = body.orientation;
} body.advance(
this.state.orientation
);
body.applyToMesh(); } else {
}.bind(this)
);
this.state.rotation = 0; var temp = {
x : body.position.x,
y : body.position.y,
orientation : body.orientation
};
}.bind(this); body.position.x = backup.x;
body.position.y = backup.y;
body.position.orientation = backup.orientation;
/* backup.x = temp.x;
GameLib.CustomCode.prototype.initializeGrid = function() { backup.y = temp.y;
backup.orientation = temp.orientation;
}
this.grid = []; body.applyToMesh();
}.bind(this)
);
for (var x = 0; x < GameLib.CustomCode.GRID_WIDTH; x++) { }.bind(this);
this.grid[x] = [];
for (var y = 0; y < GameLib.CustomCode.GRID_HEIGHT; y++) {
this.grid[x][y] = {
mesh : null,
direction : {
x : 0,
y : 0
}
}
}
}
}.bind(this); /*
*/ GameLib.CustomCode.prototype.initializeGrid = function() {
GameLib.Event.Subscribe( this.grid = [];
GameLib.Event.GAME_START,
function() {
/** for (var x = 0; x < GameLib.CustomCode.GRID_WIDTH; x++) {
* Remove all existing snake parts this.grid[x] = [];
*/ for (var y = 0; y < GameLib.CustomCode.GRID_HEIGHT; y++) {
this.snake.map( this.grid[x][y] = {
function(body) { mesh : null,
body.geometry = null; direction : {
body.materials = null; x : 0,
body.remove(); y : 0
} }
) }
}
}
this.state = { }.bind(this);
direction : { */
x : 0,
y : 1
},
rotation : 0
};
this.snake = [ GameLib.Event.Subscribe(
new GameLib.CustomCode.SnakeBody( GameLib.Event.GAME_START,
this.meshBreadEnd, function() {
{
x : 4,
y : 4
},
0
),
new GameLib.CustomCode.SnakeBody(
this.meshBreadPatty,
{
x : 4,
y : 3
},
0
),
new GameLib.CustomCode.SnakeBody(
this.meshBreadPatty,
{
x : 4,
y : 2
},
0
),
new GameLib.CustomCode.SnakeBody(
this.meshBreadEnd,
{
x : 4,
y : 1
},
Math.PI
)
];
/** /**
* Cleanup grid * Remove all existing snake parts
* */
this.grid.map( this.snake.map(
function(x) { function(body) {
body.geometry = null;
body.materials = null;
body.remove();
}
)
this.state = {
orientation : GameLib.CustomCode.ORIENTATION_UP
};
this.snake = [
new GameLib.CustomCode.SnakeBody(
this.meshBreadHead,
{
x : 4,
y : 4
},
0
),
new GameLib.CustomCode.SnakeBody(
this.meshBreadPatty,
{
x : 4,
y : 3
},
0
),
new GameLib.CustomCode.SnakeBody(
this.meshBreadPatty,
{
x : 4,
y : 2
},
0
),
new GameLib.CustomCode.SnakeBody(
this.meshBreadTail,
{
x : 4,
y : 1
},
0
)
];
/**
* Cleanup grid
*
this.grid.map(
function(x) {
x.map( x.map(
function(y) { function(y) {
y.mesh.geometry = null; y.mesh.geometry = null;
@ -302,43 +330,44 @@ GameLib.Event.Subscribe(
} }
); );
} }
); );
this.initializeGrid(); this.initializeGrid();
*/ */
/** /**
* Other Settings * Other Settings
*/ */
this.speed = GameLib.CustomCode.SPEED_INITIAL; this.speed = GameLib.CustomCode.SPEED_INITIAL;
/** /**
* Re-initialize our other custom code components * Re-initialize our other custom code components
*/ */
this.beforeRender.initialized = false; this.beforeRender.initialized = false;
this.beforeRender.entityLoaded = this; this.beforeRender.entityLoaded = this;
this.keyUp.initialized = false; this.keyUp.initialized = false;
this.keyUp.entityLoaded = this; this.keyUp.entityLoaded = this;
console.log('starting game snake'); console.log('starting game snake');
}.bind(this) }.bind(this)
); );
GameLib.Event.Subscribe( GameLib.Event.Subscribe(
GameLib.Event.GAME_OVER, GameLib.Event.GAME_OVER,
function() { function() {
this.keyUp.initialized = false; this.keyUp.initialized = false;
this.keyUp.entityLoaded = null; this.keyUp.entityLoaded = null;
this.beforeRender.initialized = false; this.beforeRender.initialized = false;
this.beforeRender.entityLoaded = null; this.beforeRender.entityLoaded = null;
console.log('starting game snake'); console.log('starting game snake');
}.bind(this) }.bind(this)
) )
GameLib.Event.Emit(GameLib.Event.GAME_LOADED); GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
//@ sourceURL=entityLoaded.js //@ sourceURL=entityLoaded.js
}