Fix flickering issue by having singleton sprites; add loading priority; load levels from source
This commit is contained in:
parent
14ddb31441
commit
dee568c51d
BIN
assets/.DS_Store
vendored
BIN
assets/.DS_Store
vendored
Binary file not shown.
BIN
assets/image/.DS_Store
vendored
BIN
assets/image/.DS_Store
vendored
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
Binary file not shown.
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.3 KiB |
42
index.js
42
index.js
@ -1,11 +1,53 @@
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const MAP_LAYERS=2;
|
||||
|
||||
const app = express();
|
||||
app.route('/').get((req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'index.html'));
|
||||
});
|
||||
|
||||
app.route('/levels').get((req, res) => {
|
||||
fs.readFile(path.join(__dirname, 'level-all.bbiy'), 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
const levels = [];
|
||||
const lines = data.split('\r\n');
|
||||
do {
|
||||
const levelName = lines.shift();
|
||||
if (!levelName) {
|
||||
break;
|
||||
}
|
||||
const [xDim, yDim] = lines.shift().split(' x ').map((x) => parseInt(x));
|
||||
|
||||
let level = Array(yDim).fill(null).map(() => Array(xDim).fill(null).map(() => []));
|
||||
for (let i = 0; i < MAP_LAYERS; i++) {
|
||||
for (let y = 0; y < yDim; y++) {
|
||||
const line = lines.shift().split('');
|
||||
for (let x = 0; x < xDim; x++) {
|
||||
if (line[x] !== ' ') {
|
||||
level[y][x].push(line[x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
levels.push({
|
||||
levelName,
|
||||
gridSize: {xDim, yDim},
|
||||
level,
|
||||
});
|
||||
} while (lines.length);
|
||||
|
||||
// Send the array of objects
|
||||
res.send(levels);
|
||||
});
|
||||
});
|
||||
|
||||
app.use(express.static('.'));
|
||||
|
||||
app.listen(3000, () => {
|
||||
|
16
src/bootstrap.js
vendored
16
src/bootstrap.js
vendored
@ -1,12 +1,13 @@
|
||||
game.bootstrap = (() => {
|
||||
const scripts = [
|
||||
{ src: ['src/utils/objectEquivalence.js', 'src/utils/unitizeVector.js', 'src/utils/clamp.js'], id: 'utils'},
|
||||
{ src: ['src/render/graphics.js'], id: 'graphics' },
|
||||
{ src: ['src/utils/objectEquivalence.js', 'src/utils/unitizeVector.js', 'src/utils/clamp.js', 'src/utils/loadLevel.js'], id: 'utils'},
|
||||
{ src: ['src/render/graphics.js', 'src/render/sprites.js'], id: 'graphics' },
|
||||
{ src: ['src/components/component.js'], id: 'component' },
|
||||
{
|
||||
src: [
|
||||
'src/components/position.js', 'src/components/momentum.js', 'src/components/gridPosition.js',
|
||||
'src/components/appearence.js', 'src/components/controllable.js', 'src/components/pushable.js',
|
||||
'src/components/loadPriority.js',
|
||||
],
|
||||
id: 'components'
|
||||
},
|
||||
@ -69,6 +70,7 @@ game.bootstrap = (() => {
|
||||
}
|
||||
|
||||
const loadAssets = function() {
|
||||
game.assets = {};
|
||||
const promises = [];
|
||||
for (let key in assets) {
|
||||
promises.push(loadAsset(assets[key], (asset) => {
|
||||
@ -82,8 +84,14 @@ game.bootstrap = (() => {
|
||||
}));
|
||||
}
|
||||
|
||||
game.assets = {};
|
||||
loadAssets().then(() => {
|
||||
const loadLevels = function() {
|
||||
game.levels = [];
|
||||
fetch('/levels')
|
||||
.then((r) => r.json())
|
||||
.then((r) => game.levels = r);
|
||||
}
|
||||
|
||||
Promise.all([loadAssets(), loadLevels()]).then(() => {
|
||||
loadScripts(() => game.initialize());
|
||||
})
|
||||
})();
|
||||
|
1
src/components/loadPriority.js
Normal file
1
src/components/loadPriority.js
Normal file
@ -0,0 +1 @@
|
||||
game.components.LoadPriority = ({priority}) => game.Component('loadPriority', {priority});
|
@ -1,13 +1,11 @@
|
||||
game.createBigBlue = () => {
|
||||
const bigBlue = game.Entity();
|
||||
bigBlue.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
bigBlue.addComponent(game.components.LoadPriority({priority: 1}));
|
||||
bigBlue.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
bigBlue.sprite = game.graphics.Sprite({
|
||||
image: game.assets.bigblue,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
bigBlue.sprite = game.sprites.bigBlue;
|
||||
|
||||
// TODO: Remove this
|
||||
bigBlue.addComponent(game.components.Controllable({controls: ['left', 'right', 'up', 'down']}));
|
||||
|
||||
return bigBlue;
|
||||
}
|
@ -1,13 +1,7 @@
|
||||
game.createFlag = () => {
|
||||
const flag = game.Entity();
|
||||
flag.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
flag.addComponent(game.components.LoadPriority({priority: 2}));
|
||||
flag.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
flag.sprite = game.graphics.Sprite({
|
||||
image: game.assets.flag,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
flag.sprite = game.sprites.flag;
|
||||
return flag;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createFloor = () => {
|
||||
const floor = game.Entity();
|
||||
floor.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
floor.addComponent(game.components.LoadPriority({priority: 5}));
|
||||
floor.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
floor.sprite = game.graphics.Sprite({
|
||||
image: game.assets.floor,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
floor.sprite = game.sprites.floor;
|
||||
return floor;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createGrass = () => {
|
||||
const grass = game.Entity();
|
||||
grass.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
grass.addComponent(game.components.LoadPriority({priority: 6}));
|
||||
grass.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
grass.sprite = game.graphics.Sprite({
|
||||
image: game.assets.grass,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
grass.sprite = game.sprites.grass;
|
||||
return grass;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createHedge = () => {
|
||||
const hedge = game.Entity();
|
||||
hedge.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
hedge.addComponent(game.components.LoadPriority({priority: 6}));
|
||||
hedge.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
hedge.sprite = game.graphics.Sprite({
|
||||
image: game.assets.hedge,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
hedge.sprite = game.sprites.hedge;
|
||||
return hedge;
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
game.createLiquid = () => {
|
||||
// TODO: Split this into two entities: water and lava
|
||||
const liquid = game.Entity();
|
||||
liquid.addComponent(game.components.LoadPriority({priority: 5}));
|
||||
liquid.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
liquid.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
liquid.sprite = game.graphics.Sprite({
|
||||
|
@ -1,13 +1,11 @@
|
||||
game.createRock = () => {
|
||||
const rock = game.Entity();
|
||||
rock.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
rock.addComponent(game.components.LoadPriority({priority: 2}));
|
||||
rock.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
rock.sprite = game.graphics.Sprite({
|
||||
image: game.assets.rock,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
rock.sprite = game.sprites.rock;
|
||||
|
||||
//TODO: Remove this
|
||||
rock.addComponent(game.components.Pushable());
|
||||
|
||||
return rock;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWall = () => {
|
||||
const wall = game.Entity();
|
||||
wall.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wall.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wall.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wall.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wall,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wall.sprite = game.sprites.wall;
|
||||
return wall;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordBigBlue = () => {
|
||||
const wordBigBlue = game.Entity();
|
||||
wordBigBlue.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordBigBlue.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordBigBlue.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordBigBlue.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordBigBlue,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordBigBlue.sprite = game.sprites.wordBigBlue;
|
||||
return wordBigBlue;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordFlag = () => {
|
||||
const wordFlag = game.Entity();
|
||||
wordFlag.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordFlag.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordFlag.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordFlag.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordFlag,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordFlag.sprite = game.sprites.wordFlag;
|
||||
return wordFlag;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordIs = () => {
|
||||
const wordIs = game.Entity();
|
||||
wordIs.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordIs.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordIs.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordIs.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordIs,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordIs.sprite = game.sprites.wordIs;
|
||||
return wordIs;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordKill = () => {
|
||||
const wordKill = game.Entity();
|
||||
wordKill.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordKill.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordKill.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordKill.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordKill,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordKill.sprite = game.sprites.wordKill;
|
||||
return wordKill;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordLava = () => {
|
||||
const wordLava = game.Entity();
|
||||
wordLava.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordLava.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordLava.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordLava.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordLava,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordLava.sprite = game.sprites.wordLava;
|
||||
return wordLava;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordPush = () => {
|
||||
const wordPush = game.Entity();
|
||||
wordPush.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordPush.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordPush.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordPush.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordPush,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordPush.sprite = game.sprites.wordPush;
|
||||
return wordPush;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordRock = () => {
|
||||
const wordRock = game.Entity();
|
||||
wordRock.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordRock.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordRock.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordRock.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordRock,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordRock.sprite = game.sprites.wordRock;
|
||||
return wordRock;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordSink = () => {
|
||||
const wordSink = game.Entity();
|
||||
wordSink.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordSink.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordSink.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordSink.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordSink,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordSink.sprite = game.sprites.wordSink;
|
||||
return wordSink;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordStop = () => {
|
||||
const wordStop = game.Entity();
|
||||
wordStop.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordStop.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordStop.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordStop.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordStop,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordStop.sprite = game.sprites.wordStop;
|
||||
return wordStop;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordWall = () => {
|
||||
const wordWall = game.Entity();
|
||||
wordWall.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordWall.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordWall.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordWall.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordWall,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordWall.sprite = game.sprites.wordWall;
|
||||
return wordWall;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordWater = () => {
|
||||
const wordWater = game.Entity();
|
||||
wordWater.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordWater.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordWater.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordWater.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordWater,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordWater.sprite = game.sprites.wordWater;
|
||||
return wordWater;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordWin = () => {
|
||||
const wordWin = game.Entity();
|
||||
wordWin.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordWin.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordWin.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordWin.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordWin,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordWin.sprite = game.sprites.wordWin;
|
||||
return wordWin;
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
game.createWordYou = () => {
|
||||
const wordYou = game.Entity();
|
||||
wordYou.addComponent(game.components.Position({x: 0, y: 0}));
|
||||
wordYou.addComponent(game.components.LoadPriority({priority: 3}));
|
||||
wordYou.addComponent(game.components.Appearance({rot: 0, width: 100, height: 100}));
|
||||
wordYou.sprite = game.graphics.Sprite({
|
||||
image: game.assets.wordYou,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
});
|
||||
wordYou.sprite = game.sprites.wordYou;
|
||||
return wordYou;
|
||||
}
|
||||
|
28
src/game.js
28
src/game.js
@ -7,37 +7,25 @@ game.loop = (timeStamp) => {
|
||||
game.systems[i].update(elapsedTime, game.entities);
|
||||
});
|
||||
|
||||
if (game.nextLevel) {
|
||||
game.loadLevel(game.nextLevel);
|
||||
game.nextLevel = false;
|
||||
}
|
||||
|
||||
requestAnimationFrame(game.loop);
|
||||
}
|
||||
|
||||
game.initialize = () => {
|
||||
[game.entities, game.config] = game.loadLevel(game.levels[0]);
|
||||
|
||||
game.systemOrder = ["render", "physics", "gridSystem", "keyboardInput"];
|
||||
game.systems = {
|
||||
render: game.system.Render(game.graphics),
|
||||
physics: game.system.Physics(),
|
||||
gridSystem: game.system.GridSystem({
|
||||
xDim: 15,
|
||||
yDim: 15,
|
||||
canvasWidth: game.canvas.width,
|
||||
canvasHeight: game.canvas.height,
|
||||
}),
|
||||
gridSystem: game.system.GridSystem({...game.config}),
|
||||
keyboardInput: game.system.KeyboardInput(),
|
||||
};
|
||||
|
||||
game.entities = {};
|
||||
|
||||
Array(10).fill(null).forEach((_, i) => {
|
||||
const bigBlue = game.createBigBlue();
|
||||
bigBlue.addComponent(game.components.GridPosition({x: Math.floor(Math.random() * 15), y: Math.floor(Math.random() * 13)}));
|
||||
bigBlue.addComponent(game.components.Controllable({controls: ['left', 'right', 'up', 'down']}));
|
||||
game.entities[bigBlue.id] = bigBlue;
|
||||
});
|
||||
|
||||
game.rock = game.createRock();
|
||||
game.rock.addComponent(game.components.Position({x: 200, y: 200}));
|
||||
game.rock.addComponent(game.components.GridPosition({x: 0, y: 0}));
|
||||
game.rock.addComponent(game.components.Pushable());
|
||||
game.entities[game.rock.id] = game.rock;
|
||||
|
||||
lastTimeStamp = performance.now()
|
||||
requestAnimationFrame(game.loop);
|
||||
|
142
src/render/sprites.js
Normal file
142
src/render/sprites.js
Normal file
@ -0,0 +1,142 @@
|
||||
game.sprites = {
|
||||
bigBlue: game.graphics.Sprite({
|
||||
image: game.assets.bigblue,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
flag: game.graphics.Sprite({
|
||||
image: game.assets.flag,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
floor: game.graphics.Sprite({
|
||||
image: game.assets.floor,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
hedge: game.graphics.Sprite({
|
||||
image: game.assets.hedge,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 250,
|
||||
}),
|
||||
grass: game.graphics.Sprite({
|
||||
image: game.assets.grass,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
rock: game.graphics.Sprite({
|
||||
image: game.assets.rock,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wall: game.graphics.Sprite({
|
||||
image: game.assets.wall,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordBigBlue: game.graphics.Sprite({
|
||||
image: game.assets.wordBigBlue,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordFlag: game.graphics.Sprite({
|
||||
image: game.assets.wordFlag,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordIs: game.graphics.Sprite({
|
||||
image: game.assets.wordIs,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordKill: game.graphics.Sprite({
|
||||
image: game.assets.wordKill,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordLava: game.graphics.Sprite({
|
||||
image: game.assets.wordLava,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordPush: game.graphics.Sprite({
|
||||
image: game.assets.wordPush,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordRock: game.graphics.Sprite({
|
||||
image: game.assets.wordRock,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordSink: game.graphics.Sprite({
|
||||
image: game.assets.wordSink,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordStop: game.graphics.Sprite({
|
||||
image: game.assets.wordStop,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordWall: game.graphics.Sprite({
|
||||
image: game.assets.wordWall,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordWater: game.graphics.Sprite({
|
||||
image: game.assets.wordWater,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordWin: game.graphics.Sprite({
|
||||
image: game.assets.wordWin,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
wordYou: game.graphics.Sprite({
|
||||
image: game.assets.wordYou,
|
||||
spriteHeight: 24,
|
||||
spriteWidth: 24,
|
||||
numFrames: 3,
|
||||
timePerFrame: 100,
|
||||
}),
|
||||
};
|
@ -1,11 +1,11 @@
|
||||
game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => {
|
||||
game.system.GridSystem = ({ xDim, yDim }) => {
|
||||
const entitiesGrid = Array(yDim).fill(null).map(() => Array(xDim).fill(null).map(() => new Map()));
|
||||
|
||||
const gridWidth = canvasWidth / xDim;
|
||||
const gridHeight = canvasHeight / yDim;
|
||||
let gridWidth = game.canvas.width / xDim;
|
||||
let gridHeight = game.canvas.height / yDim;
|
||||
|
||||
const gameCoordsToGrid = ({ x, y }) => {
|
||||
return { x: Math.floor((x+gridWidth/2) / canvasWidth * xDim), y: Math.floor((y+gridHeight/2) / canvasHeight * yDim) };
|
||||
return { x: Math.floor((x+gridWidth/2) / game.canvas.width * xDim), y: Math.floor((y+gridHeight/2) / game.canvas.height * yDim) };
|
||||
};
|
||||
|
||||
const gridCoordsToGame = ({ x, y }) => {
|
||||
@ -39,8 +39,8 @@ game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => {
|
||||
rebuildGrid(gridEntities);
|
||||
gridEntities.map((entity) => {
|
||||
if (entity.hasComponent("appearance")) {
|
||||
entity.components.appearance.width = canvasWidth / xDim;
|
||||
entity.components.appearance.height = canvasHeight / yDim;
|
||||
entity.components.appearance.width = gridWidth;
|
||||
entity.components.appearance.height = gridHeight;
|
||||
}
|
||||
if (entity.hasComponent("position")) {
|
||||
const newGridCoords = gameCoordsToGrid(entity.components.position);
|
||||
@ -89,7 +89,9 @@ game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => {
|
||||
entity.components.momentum.dy = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
entity.addComponent(game.components.Position({...gridCoordsToGame(entity.components.gridPosition)}));
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -2,12 +2,18 @@ game.system.Render = (graphics) => {
|
||||
const update = (elapsedTime, entities) => {
|
||||
graphics.clear();
|
||||
|
||||
for (let id in entities) {
|
||||
const entity = entities[id];
|
||||
const entitiesArray = Object.keys(entities).map(key => entities[key]);
|
||||
const sortedEntities = entitiesArray.sort((a, b) => {
|
||||
const aprior = a.hasComponent("loadPriority") ? a.components.loadPriority.priority : 0;
|
||||
const bprior = b.hasComponent("loadPriority") ? b.components.loadPriority.priority : 0;
|
||||
return bprior - aprior;
|
||||
});
|
||||
|
||||
sortedEntities.forEach((entity) => {
|
||||
if (entity.sprite && entity.hasComponent("position") && entity.hasComponent("appearance")) {
|
||||
entity.sprite.draw(elapsedTime, {...entity.components.position, ...entity.components.appearance});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return { update };
|
||||
};
|
66
src/utils/loadLevel.js
Normal file
66
src/utils/loadLevel.js
Normal file
@ -0,0 +1,66 @@
|
||||
game.loadLevel = (level) => {
|
||||
const entities = {};
|
||||
const config = {...level.gridSize};
|
||||
for (let y = 0; y < level.gridSize.yDim; y++) {
|
||||
for (let x = 0; x < level.gridSize.xDim; x++) {
|
||||
const cell = level.level[y][x];
|
||||
cell.forEach((letter) => {
|
||||
let entity;
|
||||
switch (letter) {
|
||||
case 'b':
|
||||
entity = game.createBigBlue();
|
||||
break;
|
||||
case 'h':
|
||||
entity = game.createHedge();
|
||||
break;
|
||||
case 'r':
|
||||
entity = game.createRock();
|
||||
break;
|
||||
case 'f':
|
||||
entity = game.createFlag();
|
||||
break;
|
||||
case 'l':
|
||||
entity = game.createFloor();
|
||||
break;
|
||||
case 'w':
|
||||
entity = game.createWall();
|
||||
break;
|
||||
case 'W':
|
||||
entity = game.createWordWall();
|
||||
break;
|
||||
case 'I':
|
||||
entity = game.createWordIs();
|
||||
break;
|
||||
case 'S':
|
||||
entity = game.createWordStop();
|
||||
break;
|
||||
case 'R':
|
||||
entity = game.createWordRock();
|
||||
break;
|
||||
case 'P':
|
||||
entity = game.createWordPush();
|
||||
break;
|
||||
case 'B':
|
||||
entity = game.createWordBigBlue();
|
||||
break;
|
||||
case 'Y':
|
||||
entity = game.createWordYou();
|
||||
break;
|
||||
case 'F':
|
||||
entity = game.createWordFlag();
|
||||
break;
|
||||
case 'X':
|
||||
entity = game.createWordWin();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (entity) {
|
||||
entity.addComponent(game.components.GridPosition({x, y}));
|
||||
entities[entity.id] = entity;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return [entities, config];
|
||||
};
|
Loading…
Reference in New Issue
Block a user