Rob Colbert
2 years ago
8 changed files with 345 additions and 161 deletions
@ -0,0 +1,111 @@ |
|||
// cyberegg2077.js
|
|||
// Copyright (C) 2022 Rob Colbert @[email protected]
|
|||
// License: Apache-2.0
|
|||
|
|||
'use strict'; |
|||
|
|||
const DTP_COMPONENT_NAME = 'nice-game'; |
|||
|
|||
import { NiceImage, NiceSprite, NiceGame } from '/dist/js/lib/nice-game.js'; |
|||
|
|||
class Tex extends NiceSprite { |
|||
|
|||
constructor ( ) { |
|||
super(); |
|||
|
|||
this.input = { |
|||
moveLeft: false, |
|||
moveRight: false, |
|||
}; |
|||
|
|||
this.moveSpeed = 2; |
|||
} |
|||
|
|||
async load ( ) { return super.load('/dist/assets/img/big-baja-tex.png', 82, 128); } |
|||
|
|||
update ( ) { |
|||
if (this.input.moveLeft) { |
|||
this.position.x -= this.moveSpeed; |
|||
} |
|||
if (this.input.moveRight) { |
|||
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)); |
|||
} |
|||
|
|||
async run ( ) { |
|||
await this.startGameEngine(this.onUpdateDisplay.bind(this)); |
|||
|
|||
this.input.addKey('moveLeft', 'LeftArrow'); |
|||
this.input.addKey('moveRight', 'RightArrow'); |
|||
this.input.addKey('throwEgg', 'Space'); |
|||
|
|||
this.input.addButton('moveLeft', 'btn-move-left'); |
|||
this.input.addButton('moveRight', 'btn-move-right'); |
|||
this.input.addButton('throwEgg', 'btn-throw-egg'); |
|||
|
|||
await this.audio.setMusicStream('/dist/assets/audio/cyber_pulse.ogg'); |
|||
} |
|||
|
|||
async onStartGame (/* event */) { |
|||
this.audio.music.play(); |
|||
this.startButton.setAttribute('hidden', ''); |
|||
|
|||
this.tex.moveSpeed = 2; |
|||
} |
|||
|
|||
onUpdateDisplay (ctx) { |
|||
/* |
|||
* Update game objects, run the logic, woo. |
|||
*/ |
|||
|
|||
this.tex.update(); |
|||
|
|||
/* |
|||
* Render the game scene using painter's algorithm (back to front) |
|||
*/ |
|||
|
|||
this.background.draw(ctx, 0, 0); |
|||
|
|||
this.tex.render(ctx); |
|||
} |
|||
|
|||
async loadGameAssets ( ) { |
|||
this.background = new NiceImage(960, 540); |
|||
await this.background.load('/dist/assets/img/bg-001.jpg'); |
|||
|
|||
this.tex = new Tex(); |
|||
this.tex.position.x = 480; |
|||
this.tex.position.y = 470; |
|||
await this.tex.load(); |
|||
} |
|||
} |
|||
|
|||
window.addEventListener('load', async ( ) => { |
|||
window.game = new CyberEgg2077(); |
|||
window.game.run(); |
|||
}); |
@ -0,0 +1,54 @@ |
|||
// lib/nice-game.js
|
|||
// Copyright (C) 2022 Rob Colbert
|
|||
// License: Apache-2.0
|
|||
|
|||
'use strict'; |
|||
|
|||
import NiceLog from '/dist/js/lib/nice-log.js'; |
|||
import NiceAudio from '/dist/js/lib/nice-audio.js'; |
|||
import { NiceInput } from '/dist/js/lib/nice-input.js'; |
|||
import NiceImage from './nice-image.js'; |
|||
import NiceSprite from './nice-sprite.js'; |
|||
|
|||
class NiceGame { |
|||
|
|||
constructor (componentName) { |
|||
this.componentName = componentName; |
|||
|
|||
this.log = new NiceLog(this.componentName); |
|||
this.log.enable(true); |
|||
|
|||
this.sprites = { }; |
|||
this.eggs = [ ]; |
|||
} |
|||
|
|||
async startGameEngine (onGameUpdate) { |
|||
this.gameDisplayCanvas = document.getElementById('game-display'); |
|||
if (!this.gameDisplayCanvas) { |
|||
this.log.error('startup', 'failed to find game display canvas'); |
|||
throw new Error('failed to find game display canvas'); |
|||
} |
|||
|
|||
this.onGameUpdate = onGameUpdate; |
|||
|
|||
this.input = new NiceInput(); |
|||
this.audio = new NiceAudio(); |
|||
|
|||
this.log.info('startup', 'starting game update loop'); |
|||
this.gameDisplayCanvas.width = 960; |
|||
this.gameDisplayCanvas.height = 540; |
|||
|
|||
this.gameDisplayCtx = this.gameDisplayCanvas.getContext('2d'); |
|||
this.onUpdateDisplay = this.updateDisplay.bind(this); |
|||
window.requestAnimationFrame(this.onUpdateDisplay); |
|||
} |
|||
|
|||
updateDisplay ( ) { |
|||
if (this.onGameUpdate && (typeof this.onGameUpdate === 'function')) { |
|||
this.onGameUpdate(); |
|||
} |
|||
window.requestAnimationFrame(this.onUpdateDisplay); |
|||
} |
|||
} |
|||
|
|||
export { NiceLog, NiceAudio, NiceInput, NiceImage, NiceSprite, NiceGame }; |
@ -1,123 +0,0 @@ |
|||
// nice-app.js
|
|||
// Copyright (C) 2022 Rob Colbert
|
|||
// License: Apache-2.0
|
|||
|
|||
'use strict'; |
|||
|
|||
const DTP_COMPONENT_NAME = 'nice-app'; |
|||
|
|||
import NiceLog from '/dist/js/lib/nice-log.js'; |
|||
import NiceAudio from '/dist/js/lib/nice-audio.js'; |
|||
import NiceSprite from '/dist/js/lib/nice-sprite.js'; |
|||
import NiceImage from '/dist/js/lib/nice-image.js'; |
|||
|
|||
class Tex extends NiceSprite { |
|||
|
|||
constructor ( ) { |
|||
super(); |
|||
|
|||
this.input = { |
|||
moveLeft: false, |
|||
moveRight: false, |
|||
}; |
|||
|
|||
this.moveSpeed = 2; |
|||
} |
|||
|
|||
async load ( ) { return super.load('/dist/assets/img/big-baja-tex.png', 82, 128); } |
|||
|
|||
update ( ) { |
|||
if (this.input.moveLeft) { |
|||
this.position.x -= this.moveSpeed; |
|||
} |
|||
if (this.input.moveRight) { |
|||
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 NiceApp { |
|||
|
|||
constructor ( ) { |
|||
this.log = new NiceLog(DTP_COMPONENT_NAME); |
|||
this.log.enable(true); |
|||
|
|||
this.sprites = { }; |
|||
this.eggs = [ ]; |
|||
|
|||
this.startButton = document.getElementById('start-button'); |
|||
|
|||
this.log.info('constructor', 'CyberEgg 2077 online'); |
|||
} |
|||
|
|||
async startGameEngine ( ) { |
|||
this.gameDisplayCanvas = document.getElementById('game-display'); |
|||
if (!this.gameDisplayCanvas) { |
|||
this.log.error('startup', 'failed to find game display canvas'); |
|||
throw new Error('failed to find game display canvas'); |
|||
} |
|||
|
|||
this.initInput(); |
|||
await this.loadGameAssets(); |
|||
|
|||
this.log.info('startup', 'starting game update loop'); |
|||
this.gameDisplayCanvas.width = 960; |
|||
this.gameDisplayCanvas.height = 540; |
|||
|
|||
this.gameDisplayCtx = this.gameDisplayCanvas.getContext('2d'); |
|||
this.onUpdateDisplay = this.updateDisplay.bind(this); |
|||
window.requestAnimationFrame(this.onUpdateDisplay); |
|||
} |
|||
|
|||
async startGame ( ) { |
|||
this.audio.music.play(); |
|||
this.startButton.setAttribute('hidden', ''); |
|||
|
|||
this.tex.moveSpeed = 2; |
|||
} |
|||
|
|||
updateDisplay ( ) { |
|||
this.tex.update(); |
|||
|
|||
this.background.draw(this.gameDisplayCtx, 0, 0); |
|||
|
|||
this.tex.render(this.gameDisplayCtx); |
|||
|
|||
window.requestAnimationFrame(this.onUpdateDisplay); |
|||
} |
|||
|
|||
async loadGameAssets ( ) { |
|||
this.audio = new NiceAudio(); |
|||
await this.audio.load(); |
|||
|
|||
this.background = new NiceImage(960, 540); |
|||
await this.background.load('/dist/assets/img/bg-001.jpg'); |
|||
|
|||
this.tex = new Tex(); |
|||
this.tex.position.x = 480; |
|||
this.tex.position.y = 470; |
|||
await this.tex.load(); |
|||
} |
|||
} |
|||
|
|||
window.addEventListener('load', async ( ) => { |
|||
console.log('window:load'); |
|||
window.app = new NiceApp(); |
|||
await window.app.startGameEngine(); |
|||
}); |
Loading…
Reference in new issue