Add the beginnings of entity management
This commit is contained in:
parent
73514e1eaf
commit
2c16bc0d7a
5 changed files with 170 additions and 28 deletions
23
level/Dolphin.js
Normal file
23
level/Dolphin.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
Dolphin = function(level, x, y, z) {
|
||||
var dolphin = Entity(level, x, y, z);
|
||||
|
||||
var sprite = Renderer.animation([
|
||||
"dolphin3",
|
||||
"dolphin2",
|
||||
"dolphin1",
|
||||
"dolphin2",
|
||||
"dolphin3",
|
||||
"dolphin4",
|
||||
"dolphin5",
|
||||
"dolphin4"
|
||||
]).speed(20);
|
||||
|
||||
dolphin.addSprite("normal", sprite);
|
||||
dolphin.currentSprite("normal");
|
||||
|
||||
dolphin.think = function() {
|
||||
dolphin.position.x = Math.cos(level.ticCount/100)*600 + 500;
|
||||
};
|
||||
|
||||
return dolphin;
|
||||
};
|
42
level/Entity.js
Normal file
42
level/Entity.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
Entity = function(level, x, y, z) {
|
||||
var entity = {};
|
||||
|
||||
entity.position = { "x": x, "y": y, "z": z };
|
||||
|
||||
// Manage animations/sprites.
|
||||
var anims = {};
|
||||
var currentAnim;
|
||||
|
||||
entity.addSprite = function(name, sprite) {
|
||||
anims[name] = sprite;
|
||||
level.addSprite(sprite);
|
||||
}
|
||||
|
||||
entity.currentSprite = function(name) {
|
||||
if (!name) {
|
||||
return currentAnim;
|
||||
}
|
||||
|
||||
if (name == currentAnim) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (entity.activeSprite) {
|
||||
entity.activeSprite.visible = false;
|
||||
if (entity.activeSprite.stop) {
|
||||
entity.activeSprite.stop();
|
||||
}
|
||||
}
|
||||
|
||||
entity.activeSprite = anims[name];
|
||||
|
||||
entity.activeSprite.visible = true;
|
||||
if (entity.activeSprite.play) {
|
||||
entity.activeSprite.gotoAndPlay(0);
|
||||
}
|
||||
|
||||
currentAnim = name;
|
||||
}
|
||||
|
||||
return entity;
|
||||
};
|
117
level/Level.js
117
level/Level.js
|
@ -1,43 +1,122 @@
|
|||
Level = function() {
|
||||
Level = function(levelName) {
|
||||
var level = Scene();
|
||||
|
||||
// A set of 512x512 blocks that objects are contained in.
|
||||
var camera = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0,
|
||||
angle: 0
|
||||
};
|
||||
|
||||
// A set of 512x512 blocks that entities are contained in.
|
||||
// Blocks two grid spaces away (in a square, corners excluded) from the camera
|
||||
// are thunk and rendered in a frame.
|
||||
var objectGrid = {};
|
||||
var entityGrid = {};
|
||||
|
||||
function getGridIndex(x, y) {
|
||||
return Math.floor(x/512) + Math.floor(y/512)*512;
|
||||
// If a map is bigger than 200k pixels one way then you have other problems.
|
||||
}
|
||||
|
||||
var sprite;
|
||||
var dolphin;
|
||||
|
||||
level.init = function() {
|
||||
sprite = Renderer.animation([
|
||||
"dolphin3",
|
||||
"dolphin2",
|
||||
"dolphin1",
|
||||
"dolphin2",
|
||||
"dolphin3",
|
||||
"dolphin4",
|
||||
"dolphin5",
|
||||
"dolphin4"
|
||||
]).speed(20);
|
||||
sprite.position.set(200,200);
|
||||
level.addSprite(sprite);
|
||||
dolphin = Dolphin(level, 250, 0, 0);
|
||||
console.log(dolphin);
|
||||
placeEntityInGrid(dolphin);
|
||||
}
|
||||
|
||||
level.think = function() {
|
||||
sprite.x = Math.cos(level.ticCount/40)*100+250;
|
||||
eachEntity(function(entity) {
|
||||
if (entity.think) {
|
||||
entity.think();
|
||||
}
|
||||
placeEntityInGrid(entity);
|
||||
});
|
||||
}
|
||||
|
||||
level.show = function(frames) {
|
||||
|
||||
level.render = function(frames) {
|
||||
eachEntity(function(entity) {
|
||||
if (!entity.activeSprite) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Start with the angle and distance for the entity.
|
||||
var xdist, ydist,
|
||||
angle, distance;
|
||||
xdist = entity.position.x - camera.x;
|
||||
ydist = entity.position.y - camera.y;
|
||||
angle = (Math.atan2(ydist, xdist) - camera.angle + Math.PI) % (2 * Math.PI) - Math.PI;
|
||||
distance = Math.sqrt(ydist*ydist + xdist*xdist);
|
||||
|
||||
if (distance > 1024 || distance < 1 || Math.abs(angle) > Math.PI/2) {
|
||||
// Not in the viewport.
|
||||
entity.activeSprite.visible = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// In the viewport.
|
||||
entity.activeSprite.visible = true;
|
||||
|
||||
distance *= Math.cos(angle);
|
||||
|
||||
// Get screen position and scale.
|
||||
var scrX, scrY, scrScale;
|
||||
scrX = (angle * 1000 / Math.PI) + 250;
|
||||
scrScale = 250/distance;
|
||||
scrY = (entity.position.z - camera.z)*scrScale + 150;
|
||||
|
||||
// Set sprite position.
|
||||
entity.activeSprite.position.set(scrX, scrY);
|
||||
entity.activeSprite.scale.set(scrScale, scrScale);
|
||||
entity.activeSprite.alpha = Math.min(4 - (distance/256), 1);
|
||||
});
|
||||
}
|
||||
|
||||
level.end = function() {
|
||||
|
||||
}
|
||||
|
||||
function placeEntityInGrid(entity) {
|
||||
var grid;
|
||||
var gridpos = getGridIndex(entity.position.x, entity.position.y);
|
||||
if (entity.gridPosition != gridpos) {
|
||||
// Remove from old.
|
||||
if (entity.gridPosition !== undefined) {
|
||||
grid = entityGrid[entity.gridPosition];
|
||||
var pos = grid.indexOf(entity);
|
||||
grid.splice(pos, 1);
|
||||
}
|
||||
|
||||
// Add to new.
|
||||
grid = (entityGrid[gridpos] || (entityGrid[gridpos] = []));
|
||||
entity.gridPosition = gridpos;
|
||||
grid.push(entity);
|
||||
}
|
||||
}
|
||||
|
||||
function eachEntity(callback) {
|
||||
const offsets = [
|
||||
-1025, -1024, -1023,
|
||||
-514, -513, -512, -511, -510,
|
||||
-2, -1, 0, 1, 2,
|
||||
510, 511, 512, 513, 514,
|
||||
1023, 1024, 1025
|
||||
];
|
||||
var pos = getGridIndex(camera.x, camera.y); // todo use camera position
|
||||
|
||||
// Build up an array separately so that we can modify the entity arrays during the callback.
|
||||
var objs = [];
|
||||
offsets.forEach(function(offset) {
|
||||
if (entityGrid[pos+offset]) {
|
||||
entityGrid[pos+offset].forEach(function(entity) {
|
||||
objs.push(entity);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
objs.forEach(callback);
|
||||
}
|
||||
|
||||
return level;
|
||||
}
|
|
@ -10,18 +10,22 @@ Renderer = (function() {
|
|||
resolution: 1
|
||||
});
|
||||
|
||||
PIXI.SCALE_MODES.DEFAULT = PIXI.SCALE_MODES.NEAREST;
|
||||
|
||||
document.body.appendChild(renderer.view);
|
||||
|
||||
PIXI.loader.add("assets/sprites.json").load(next);
|
||||
}
|
||||
|
||||
Renderer.sprite = function(name) {
|
||||
return new PIXI.Sprite(PIXI.utils.TextureCache[name]);
|
||||
var sprite = new PIXI.Sprite(PIXI.utils.TextureCache[name]);
|
||||
sprite.anchor.set(0.5, 0.5);
|
||||
return sprite;
|
||||
}
|
||||
|
||||
Renderer.animation = function(sprites) {
|
||||
var anim = PIXI.extras.MovieClip.fromFrames(sprites);
|
||||
anim.play();
|
||||
anim.anchor.set(0.5, 0.5);
|
||||
anim.speed = function(speed) {
|
||||
anim.animationSpeed = speed/60;
|
||||
return anim;
|
||||
|
|
|
@ -1,13 +1,7 @@
|
|||
Scene = function() {
|
||||
var scene = {};
|
||||
|
||||
scene.stage = new PIXI.ParticleContainer(512, {
|
||||
position: true,
|
||||
rotation: true,
|
||||
scale: true,
|
||||
alpha: true,
|
||||
uvs: true
|
||||
});
|
||||
scene.stage = new PIXI.Container();
|
||||
|
||||
scene.ticCount = 0; // How many frames has this scene run?
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue