Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,325 changes: 280 additions & 6,045 deletions dist/skifree.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions dist/skifree.min.js

Large diffs are not rendered by default.

269 changes: 133 additions & 136 deletions js/lib/game.js
Original file line number Diff line number Diff line change
@@ -1,161 +1,158 @@
var SpriteArray = require('./spriteArray');
var EventedLoop = require('eventedloop');

(function (global) {
function Game (mainCanvas, player) {
var staticObjects = new SpriteArray();
var movingObjects = new SpriteArray();
var uiElements = new SpriteArray();
var dContext = mainCanvas.getContext('2d');
var mouseX = dContext.getCentreOfViewport();
var mouseY = 0;
var paused = false;
var that = this;
var beforeCycleCallbacks = [];
var afterCycleCallbacks = [];
var gameLoop = new EventedLoop();

this.addStaticObject = function (sprite) {
staticObjects.push(sprite);
};

this.addStaticObjects = function (sprites) {
sprites.forEach(this.addStaticObject.bind(this));
};

this.addMovingObject = function (movingObject, movingObjectType) {
if (movingObjectType) {
staticObjects.onPush(function (obj) {
if (obj.data && obj.data.hitBehaviour[movingObjectType]) {
obj.onHitting(movingObject, obj.data.hitBehaviour[movingObjectType]);
}
}, true);
}
import SpriteArray from './spriteArray';
import EventedLoop from 'eventedloop';

class Game {
constructor(mainCanvas, player) {
this.mainCanvas = mainCanvas;
this.player = player;

this.staticObjects = new SpriteArray();
this.movingObjects = new SpriteArray();
this.uiElements = new SpriteArray();
this.dContext = this.mainCanvas.getContext('2d');
this.mouseX = this.dContext.getCentreOfViewport();
this.mouseY = 0;
this.paused = false;
this.beforeCycleCallbacks = [];
this.afterCycleCallbacks = [];
this.gameLoop = new EventedLoop();

this.player.setMapPosition(0, 0);
this.player.setMapPositionTarget(0, -10);
this.dContext.followSprite(this.player);

this.intervalNum = 0;

this.gameLoop.on('20', this.cycle.bind(this));
this.gameLoop.on('20', this.draw.bind(this));
}

movingObjects.push(movingObject);
};
addStaticObject(sprite) {
this.staticObjects.push(sprite);
}

this.addUIElement = function (element) {
uiElements.push(element);
};
addStaticObjects(sprites) {
sprites.forEach(this.addStaticObject.bind(this));
}

this.beforeCycle = function (callback) {
beforeCycleCallbacks.push(callback);
};
addMovingObject(movingObject, movingObjectType) {
if (movingObjectType) {
this.staticObjects.onPush(function (obj) {
if (obj.data && obj.data.hitBehaviour[movingObjectType]) {
obj.onHitting(movingObject, obj.data.hitBehaviour[movingObjectType]);
}
}, true);
}

this.afterCycle = function (callback) {
afterCycleCallbacks.push(callback);
};
this.movingObjects.push(movingObject);
}

this.setMouseX = function (x) {
mouseX = x;
};
addUIElement(element) {
this.uiElements.push(element);
}

this.setMouseY = function (y) {
mouseY = y;
};
beforeCycle(callback) {
this.beforeCycleCallbacks.push(callback);
}

player.setMapPosition(0, 0);
player.setMapPositionTarget(0, -10);
dContext.followSprite(player);
afterCycle(callback) {
this.afterCycleCallbacks.push(callback);
}

var intervalNum = 0;
setMouseX(x) {
this.mouseX = x;
}

this.cycle = function () {
beforeCycleCallbacks.each(function(c) {
c();
});
setMouseY(y) {
this.mouseY = y;
}

// Clear canvas
var mouseMapPosition = dContext.canvasPositionToMapPosition([mouseX, mouseY]);
cycle() {
this.beforeCycleCallbacks.each(function(c) {
c();
});

if (!player.isJumping) {
player.setMapPositionTarget(mouseMapPosition[0], mouseMapPosition[1]);
}
// Clear canvas
var mouseMapPosition = this.dContext.canvasPositionToMapPosition([this.mouseX, this.mouseY]);

intervalNum++;
if (!this.player.isJumping) {
this.player.setMapPositionTarget(mouseMapPosition[0], mouseMapPosition[1]);
}

player.cycle();
this.intervalNum++;

movingObjects.each(function (movingObject, i) {
movingObject.cycle(dContext);
});

staticObjects.cull();
staticObjects.each(function (staticObject, i) {
if (staticObject.cycle) {
staticObject.cycle();
}
});
this.player.cycle();

uiElements.each(function (uiElement, i) {
if (uiElement.cycle) {
uiElement.cycle();
}
});
this.movingObjects.each(movingObject => {
movingObject.cycle(this.dContext);
});

this.staticObjects.cull();
this.staticObjects.each(staticObject => {
if (staticObject.cycle) {
staticObject.cycle();
}
});

afterCycleCallbacks.each(function(c) {
c();
});
};
this.uiElements.each(uiElement => {
if (uiElement.cycle) {
uiElement.cycle();
}
});

that.draw = function () {
// Clear canvas
mainCanvas.width = mainCanvas.width;
this.afterCycleCallbacks.each(c => {
c();
});
}

player.draw(dContext);
draw() {
// Clear canvas
this.mainCanvas.width = this.mainCanvas.width;

player.cycle();
this.player.draw(this.dContext);

movingObjects.each(function (movingObject, i) {
movingObject.draw(dContext);
});

staticObjects.each(function (staticObject, i) {
if (staticObject.draw) {
staticObject.draw(dContext, 'main');
}
});
this.player.cycle();

uiElements.each(function (uiElement, i) {
if (uiElement.draw) {
uiElement.draw(dContext, 'main');
}
});
};

this.start = function () {
gameLoop.start();
};

this.pause = function () {
paused = true;
gameLoop.stop();
};

this.isPaused = function () {
return paused;
};

this.reset = function () {
paused = false;
staticObjects = new SpriteArray();
movingObjects = new SpriteArray();
mouseX = dContext.getCentreOfViewport();
mouseY = 0;
player.reset();
player.setMapPosition(0, 0, 0);
this.start();
}.bind(this);

gameLoop.on('20', this.cycle);
gameLoop.on('20', this.draw);
this.movingObjects.each((movingObject, i) => {
movingObject.draw(this.dContext);
});

this.staticObjects.each((staticObject, i) => {
if (staticObject.draw) {
staticObject.draw(this.dContext, 'main');
}
});

this.uiElements.each((uiElement, i) => {
if (uiElement.draw) {
uiElement.draw(this.dContext, 'main');
}
});
}

start() {
this.gameLoop.start();
}

global.game = Game;
})( this );
pause() {
this.paused = true;
this.gameLoop.stop();
}

isPaused() {
return this.paused;
}

if (typeof module !== 'undefined') {
module.exports = this.game;
}
reset() {
paused = false;
staticObjects = new SpriteArray();
movingObjects = new SpriteArray();
mouseX = dContext.getCentreOfViewport();
mouseY = 0;
player.reset();
player.setMapPosition(0, 0, 0);
this.start();
}
}

export default Game;
Loading