|
|
@ -10,58 +10,39 @@ import { NiceImage, NiceSprite, NiceGame } from '/dist/js/lib/nice-game.js'; |
|
|
|
|
|
|
|
class Tex extends NiceSprite { |
|
|
|
|
|
|
|
constructor ( ) { |
|
|
|
constructor (game) { |
|
|
|
super(); |
|
|
|
|
|
|
|
this.input = { |
|
|
|
moveLeft: false, |
|
|
|
moveRight: false, |
|
|
|
}; |
|
|
|
|
|
|
|
this.game = game; |
|
|
|
this.moveSpeed = 2; |
|
|
|
} |
|
|
|
|
|
|
|
async load ( ) { return super.load('/dist/assets/img/big-baja-tex.png', 82, 128); } |
|
|
|
|
|
|
|
update ( ) { |
|
|
|
if (this.input.moveLeft) { |
|
|
|
const { input } = this.game; |
|
|
|
if (input.buttons.moveLeft.isPressed || input.keys.moveLeft.isPressed) { |
|
|
|
this.position.x -= this.moveSpeed; |
|
|
|
} |
|
|
|
if (this.input.moveRight) { |
|
|
|
if (input.buttons.moveRight.isPressed || input.keys.moveLeft.isPressed) { |
|
|
|
this.position.x += this.moveSpeed; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
class Egg { |
|
|
|
|
|
|
|
constructor (image) { |
|
|
|
this.image = image; |
|
|
|
this.position = { x: 0, y: 0 }; |
|
|
|
} |
|
|
|
|
|
|
|
render (ctx) { |
|
|
|
ctx.drawImage( |
|
|
|
this.sprite.image, |
|
|
|
this.position.x - (this.sprite.image.width / 2), |
|
|
|
this.position.y - (this.sprite.image.height / 2), |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
class CyberEgg2077 extends NiceGame { |
|
|
|
|
|
|
|
constructor ( ) { |
|
|
|
super(DTP_COMPONENT_NAME); |
|
|
|
this.startButton = document.getElementById('start-button'); |
|
|
|
this.startButton.addEventListener('click', this.onStartGame.bind(this)); |
|
|
|
this.mode = 'loading'; |
|
|
|
} |
|
|
|
|
|
|
|
async run ( ) { |
|
|
|
await this.startGameEngine(this.onUpdateDisplay.bind(this)); |
|
|
|
|
|
|
|
this.input.addKey('moveLeft', 'LeftArrow'); |
|
|
|
this.input.addKey('moveRight', 'RightArrow'); |
|
|
|
this.input.addKey('moveLeft', 'ArrowLeft'); |
|
|
|
this.input.addKey('moveRight', 'ArrowRight'); |
|
|
|
this.input.addKey('throwEgg', 'Space'); |
|
|
|
|
|
|
|
this.input.addButton('moveLeft', 'btn-move-left'); |
|
|
@ -69,6 +50,9 @@ class CyberEgg2077 extends NiceGame { |
|
|
|
this.input.addButton('throwEgg', 'btn-throw-egg'); |
|
|
|
|
|
|
|
await this.audio.setMusicStream('/dist/assets/audio/cyber_pulse.ogg'); |
|
|
|
await this.loadGameAssets(); |
|
|
|
|
|
|
|
this.mode = 'menu'; |
|
|
|
} |
|
|
|
|
|
|
|
async onStartGame (/* event */) { |
|
|
@ -76,21 +60,29 @@ class CyberEgg2077 extends NiceGame { |
|
|
|
this.startButton.setAttribute('hidden', ''); |
|
|
|
|
|
|
|
this.tex.moveSpeed = 2; |
|
|
|
this.mode = 'game'; |
|
|
|
} |
|
|
|
|
|
|
|
onUpdateDisplay (ctx) { |
|
|
|
/* |
|
|
|
* Update game objects, run the logic, woo. |
|
|
|
*/ |
|
|
|
switch (this.mode) { |
|
|
|
case 'loading': |
|
|
|
break; |
|
|
|
case 'menu': |
|
|
|
return this.updateMenu(ctx); |
|
|
|
case 'game': |
|
|
|
return this.updateGame(ctx); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
this.tex.update(); |
|
|
|
updateMenu (ctx) { |
|
|
|
this.background.draw(ctx, 0, 0); |
|
|
|
this.tex.render(ctx); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Render the game scene using painter's algorithm (back to front) |
|
|
|
*/ |
|
|
|
updateGame (ctx) { |
|
|
|
this.tex.update(); |
|
|
|
|
|
|
|
this.background.draw(ctx, 0, 0); |
|
|
|
|
|
|
|
this.tex.render(ctx); |
|
|
|
} |
|
|
|
|
|
|
@ -98,7 +90,7 @@ class CyberEgg2077 extends NiceGame { |
|
|
|
this.background = new NiceImage(960, 540); |
|
|
|
await this.background.load('/dist/assets/img/bg-001.jpg'); |
|
|
|
|
|
|
|
this.tex = new Tex(); |
|
|
|
this.tex = new Tex(this); |
|
|
|
this.tex.position.x = 480; |
|
|
|
this.tex.position.y = 470; |
|
|
|
await this.tex.load(); |
|
|
|