diff --git a/Ball.js b/Ball.js index 0b9b7f8..e0acddd 100644 --- a/Ball.js +++ b/Ball.js @@ -88,7 +88,6 @@ class Ball { } g2r(deg) { - console.log((360+deg)%360); return ( ((360+deg)%360) * Math.PI) / 180.0; } diff --git a/Board.js b/Board.js new file mode 100644 index 0000000..cb78045 --- /dev/null +++ b/Board.js @@ -0,0 +1,52 @@ +class Board { + constructor(ctx, key) { + this.controls = {}; + this.key = key; + this.stop = false; + this.ctx = ctx; + this.x = ctx.canvas.width; + this.y = ctx.canvas.height / 2 - 48; + + this.w = this.ctx.canvas.width; + this.h = this.ctx.canvas.height + } + + run() { + let _this = this; + this.stop = false; + this.key.setKeydown( e => { + let code = e.code; + for(var key in this.controls) { + if(key==code) this.controls[key](); + } + } ); + return new Promise((resolve, reject) => { + _this.resolve = resolve; + _this.reject = reject; + _this.loop(); + } + ); + } + + next(nextStage) { + this.stop = true; + if (this.requestID) { + cancelAnimationFrame(this.requestID); + this.requestID = null; + } + this.ctx.clearRect(0, 0, this.w, this.h); + this.resolve(nextStage); + } + + loop() { + if (this.stop) return; + + this.ctx.clearRect(0, 0, this.w, this.h); + this.update(); + this.requestID = requestAnimationFrame( ()=>this.loop() ); + } + + update() { + + } +} \ No newline at end of file diff --git a/Intro.js b/GameIntro.js similarity index 72% rename from Intro.js rename to GameIntro.js index f133767..da235ee 100644 --- a/Intro.js +++ b/GameIntro.js @@ -1,8 +1,9 @@ -class Intro { - constructor(ctx) { - this.ctx = ctx; - this.x = ctx.canvas.width; - this.y = ctx.canvas.height / 2 - 48; +class GameIntro extends Board { + constructor(ctx, key) { + super(ctx, key); + this.controls = { + 'Space': ()=>this.next(1) + } } update() { this.centerText('BreakOut', this.y, '48px', 'Consolas', 'Black'); diff --git a/GamePlay.js b/GamePlay.js new file mode 100644 index 0000000..d83d328 --- /dev/null +++ b/GamePlay.js @@ -0,0 +1,42 @@ +class GamePlay extends Board { + constructor(ctx, key) { + super(ctx, key); + + this.controls = { + 'KeyX': ()=>{ + this.balls.push(new Ball(this.ctx, this.bar)); + this.balls[this.balls.length - 1].start(); + }, + 'Space': ()=>{ + this.balls[0].moving = true; + } + } + + this.score = new Score(ctx); + this.lives = new Lives(ctx); + this.bar = new Bar(ctx, key); + this.newGame(); + } + + newGame() { + this.lives.reset(); + this.score.reset(); + this.bar.reset(); + this.balls = []; + this.balls.push(new Ball(this.ctx, this.bar)); + } + + update() { + if(this.lives.get()==0) { + gameOver.update(); + } else { + this.balls = this.balls.filter(ball => ball.update()); + if (this.balls.length==0) { + if ( !this.lives.lost() ) this.balls.push(new Ball(this.ctx, this.bar)); + } + this.bar.update(); + } + this.score.update(); + this.lives.update(); + } +} diff --git a/Lives.js b/Lives.js index 5d809b8..f163ac2 100644 --- a/Lives.js +++ b/Lives.js @@ -15,6 +15,9 @@ class Lives { this.lives--; return this.lives==0; } + get() { + return this.lives; + } update() { if (this.y != 60) this.y++; if (this.y > 0) { diff --git a/index.html b/index.html index daaa296..602f477 100644 --- a/index.html +++ b/index.html @@ -7,13 +7,18 @@ padding: 0; } + - - + + + + + + diff --git a/index.js b/index.js index 7c76180..af406ff 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ "use strict"; +// import {Intro as game_intro} from "./Intro"; +// import game_play from "./game.js"; document.addEventListener('DOMContentLoaded', init); @@ -10,70 +12,15 @@ function init() { ctx = canvas.getContext('2d'); document.body.insertBefore(canvas, document.body.childNodes[0]); - function run() { - ctx.clearRect(0, 0, canvas.width, canvas.height); - refresh(); - requestAnimationFrame(run); - } - - // ------------------------------------------------------------------- - let gstate = 0; - let key = new Key(controls); - let intro = new Intro(ctx); - let gameOver = new GameOver(ctx); - let score = new Score(ctx); - let lives = new Lives(ctx); - let bar = new Bar(ctx, key); - let balls = []; - newGame(); - - function controls(e) { - switch (gstate) { - case 0: // Waiting to start - if (e.code == 'Space') newGame(); - break; - case 1: // Playing... - if (e.code == 'Space' && balls.length>0) balls[balls.length - 1].start(); - else - if (e.code == 'KeyX') { balls.push(new Ball(ctx, bar)); balls[balls.length - 1].start(); } - break; - case 2: // Game Over - if (e.code == 'Space') newGame(); - break; + let key = new Key(), board; + function runBoard(stage) { + switch(stage) { + case 1: board = new GamePlay(ctx, key); break; + default: board = new GameIntro(ctx,key); break; } + board + .run() + .then( stage=>runBoard(stage), e=>{} );; } - - function newGame() { - gstate = 1; - lives.reset(); - score.reset(); - bar.reset(); - balls.push(new Ball(ctx, bar)); - } - - function refresh() { - switch (gstate) { - case 0: // Waiting to start - intro.update(); - break; - case 1: // Playing... - case 2: - - if(gstate==2) { - gameOver.update(); - } else { - balls = balls.filter(ball => ball.update()); - if (balls.length==0) { - if ( lives.lost() ) gstate=2; - else balls.push(new Ball(ctx, bar)); - } - bar.update(); - } - score.update(); - lives.update(); - break; - } - } - - run(); + runBoard(0); }