split up game code using new es5 syntax or whatever
This commit is contained in:
parent
b237b857c7
commit
506e6be65d
5 changed files with 93 additions and 82 deletions
24
common/game_basic.js
Normal file
24
common/game_basic.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
class GameBasic {
|
||||||
|
constructor() {
|
||||||
|
let player = new Player(true);
|
||||||
|
let entities = [player];
|
||||||
|
|
||||||
|
for (let i = 0; i < 50; i++) {
|
||||||
|
entities.push(new Player())
|
||||||
|
}
|
||||||
|
|
||||||
|
this.player = player;
|
||||||
|
this.entities = entities;
|
||||||
|
}
|
||||||
|
main() {
|
||||||
|
let { entities } = this;
|
||||||
|
|
||||||
|
for (let ent of entities) {
|
||||||
|
ent.handleTick(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
init() {
|
||||||
|
let that = this;
|
||||||
|
setInterval(function () { that.main() }, 1000 / 60);
|
||||||
|
}
|
||||||
|
}
|
2
index.js
2
index.js
|
@ -4,6 +4,8 @@ import expressW from "express-ws";
|
||||||
var app = express();
|
var app = express();
|
||||||
expressW(app);
|
expressW(app);
|
||||||
|
|
||||||
|
|
||||||
|
app.use('/js',express.static('./common'));
|
||||||
app.use(express.static('./static'));
|
app.use(express.static('./static'));
|
||||||
|
|
||||||
app.ws('/', function (ws, req) {
|
app.ws('/', function (ws, req) {
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<script src='js/player.js'></script>
|
<script src='js/player.js'></script>
|
||||||
|
<script src='js/game_basic.js'></script>
|
||||||
<script src='js/index.js'></script>
|
<script src='js/index.js'></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,10 @@ const assets = [
|
||||||
'assets/head.svg',
|
'assets/head.svg',
|
||||||
];
|
];
|
||||||
|
|
||||||
function Game() {
|
class Game extends GameBasic {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
let assetsIn = {};
|
let assetsIn = {};
|
||||||
|
|
||||||
for (let asset in assets) {
|
for (let asset in assets) {
|
||||||
|
@ -13,36 +16,14 @@ function Game() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let canvas = document.querySelector("#canvas");
|
let canvas = document.querySelector("#canvas");
|
||||||
let player = new Player(true);
|
|
||||||
let entities = [player];
|
|
||||||
|
|
||||||
for (let i = 0; i < 50; i++) {
|
|
||||||
entities.push(new Player())
|
|
||||||
}
|
|
||||||
|
|
||||||
canvas.width = canvas.height = cs;
|
canvas.width = canvas.height = cs;
|
||||||
|
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.ctx = canvas.getContext("2d");
|
this.ctx = canvas.getContext("2d");
|
||||||
this.assetsIn = assetsIn;
|
this.assetsIn = assetsIn;
|
||||||
this.player = player;
|
|
||||||
this.entities = entities;
|
|
||||||
}
|
}
|
||||||
|
render () {
|
||||||
Game.prototype.main = function () {
|
|
||||||
let { entities, player } = this;
|
|
||||||
|
|
||||||
if (player.health <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let ent of entities) {
|
|
||||||
ent.handleTick(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: move into its own file
|
|
||||||
Game.prototype.render = function () {
|
|
||||||
let { ctx, assetsIn, entities, player } = this;
|
let { ctx, assetsIn, entities, player } = this;
|
||||||
|
|
||||||
ctx.clearRect(0, 0, cs, cs);
|
ctx.clearRect(0, 0, cs, cs);
|
||||||
|
@ -75,12 +56,10 @@ Game.prototype.render = function () {
|
||||||
ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
|
ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ui() {
|
||||||
Game.prototype.ui = function() {
|
|
||||||
document.querySelector('.ui-text').textContent = `HP: ${this.player.health}`
|
document.querySelector('.ui-text').textContent = `HP: ${this.player.health}`
|
||||||
}
|
}
|
||||||
|
click() {
|
||||||
Game.prototype.click = function () {
|
|
||||||
let { player } = this;
|
let { player } = this;
|
||||||
|
|
||||||
if (player.health <= 0) {
|
if (player.health <= 0) {
|
||||||
|
@ -89,11 +68,16 @@ Game.prototype.click = function () {
|
||||||
player.bump();
|
player.bump();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
init(){
|
||||||
|
super.init();
|
||||||
|
|
||||||
|
setInterval(function () { game.render() }, 1000 / 60);
|
||||||
|
setInterval(function () { game.ui() }, 1000 / 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var game = new Game();
|
var game = new Game();
|
||||||
|
|
||||||
setInterval(function () { game.main() }, 1000 / 60);
|
game.init();
|
||||||
setInterval(function () { game.render() }, 1000 / 60);
|
|
||||||
setInterval(function () { game.ui() }, 1000 / 10);
|
|
||||||
|
|
||||||
game.canvas.onclick = () => game.click();
|
game.canvas.onclick = () => game.click();
|
Loading…
Reference in a new issue