From afc37b9981a18b7fa7d26a485d4494da986f30f2 Mon Sep 17 00:00:00 2001 From: Marak Date: Sat, 9 Mar 2024 03:53:12 -0500 Subject: [PATCH] Adds `Coin` plugin `Entity` - Reduces default `maxSpeed` from 9999 -> 3 `CrossWindow` - Updates debugger configuration API `Game` - Renaming `randomColor()` -> `getRandomColor()` --- mantra-client/public/img/game/env/coin.png | Bin 0 -> 675 bytes mantra-game/Game.js | 4 ++ mantra-game/plugins.js | 4 +- mantra-game/plugins/client/defaultAssets.js | 29 ++++++-- mantra-game/plugins/coin/Coin.js | 68 ++++++++++++++++++ .../plugins/crosswindow/CrossWindow.js | 45 +++++++++--- .../plugins/entity/lib/createEntity.js | 2 +- .../plugins/entity/lib/inflateEntity.js | 5 +- .../physics-matter/lib/onAfterUpdate.js | 2 +- 9 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 mantra-client/public/img/game/env/coin.png create mode 100644 mantra-game/plugins/coin/Coin.js diff --git a/mantra-client/public/img/game/env/coin.png b/mantra-client/public/img/game/env/coin.png new file mode 100644 index 0000000000000000000000000000000000000000..39bb2ced46e91f91012f116e0221c29a01e2682b GIT binary patch literal 675 zcmV;U0$lxxP)Px%T}ebiR9HvtSM6@oFcdwB?QT(Dh8AgG+VcP%-5(x>O{;htq)lB(OM~E5m_pMy zp04A#j@va2e*h8xNi6&3+*&7{h8<}t%^WWF&0KRrc08S1&QZ7d=6>2*$oxda#zn@T8Gi-j)X zU4u$V7DWx;5}<3APG6Y|?*#%V_&qh<5zu8oR|4G~&|QN6~En2tv^}{8ztC63YSW>g%Yf` zQsa>d&KZ=%)6GCDKN3Ea4Kz$fP*l`ez0{Z-3(nK0`_B+uKrTjZHnr&p%U+-*0RT$; zhX>otMHd&94Bo=`b-B|HZ)h~I!U literal 0 HcmV?d00001 diff --git a/mantra-game/Game.js b/mantra-game/Game.js index 03b87b2d..1ede2797 100644 --- a/mantra-game/Game.js +++ b/mantra-game/Game.js @@ -412,6 +412,10 @@ class Game { return typeof color === 'number' ? `#${color.toString(16)}` : color; } + getRandomColor( format = 'int' ) { + return this.randomColor(format); + } + randomColor(format = 'int') { let color; diff --git a/mantra-game/plugins.js b/mantra-game/plugins.js index 628fbcf3..4e0d72a1 100644 --- a/mantra-game/plugins.js +++ b/mantra-game/plugins.js @@ -63,12 +63,13 @@ import Lifetime from './plugins/lifetime/Lifetime.js'; // Game Objects import Border from './plugins/border/Border.js'; -import GravityWell from './plugins/gravitywell/GravityWell.js'; import Bomb from './plugins/bomb/Bomb.js'; import Boomerang from './plugins/boomerang/Boomerang.js'; import Bullet from './plugins/bullet/Bullet.js'; import Block from './plugins/block/Block.js'; +import Coin from './plugins/coin/Coin.js'; import Flame from './plugins/flame/Flame.js'; +import GravityWell from './plugins/gravitywell/GravityWell.js'; import Hexapod from './plugins/hexapod/Hexapod.js'; import Key from './plugins/key/Key.js'; import Platform from './plugins/platform/Platform.js'; @@ -179,6 +180,7 @@ let plugins = { Bomb, Boomerang, Bullet, + Coin, Flame, Sword, // UI Objects diff --git a/mantra-game/plugins/client/defaultAssets.js b/mantra-game/plugins/client/defaultAssets.js index 63895fbe..4bfb11db 100644 --- a/mantra-game/plugins/client/defaultAssets.js +++ b/mantra-game/plugins/client/defaultAssets.js @@ -17,12 +17,31 @@ let defaultAssets = { 'tile-entrance': '/img/game/tiles/tile-entrance.png', //'fire': '/img/game/env/loz_fire.png', // 'flare': '/img/game/particle/flare.png', - //'planet-express-base': '/img/game/env/planet-express-base.png', + //'planet-express-base': '/img/game/env/planet-express-base.png', // 'robot-arms-apartment': '/img/game/env/robot-arms-apartment.png', // '3d-homer': '/img/game/env/3d-homer.gif', 'demon': '/img/game/npc/demon.gif', 'warp-to-home': '/img/game/env/warp-to-mantra-home-256.png', 'ayyo-key': '/img/game/env/ayyo-key-medium.png', + 'ayyo-coin': { + type: 'spritesheet', + url: '/img/game/env/coin.png', + frameTags: { + coin: { + rate: 32, + frames: [ + { x: 0, y: 0 }, + /* + { x: -16, y: 0 }, + { x: -32, y: 0 }, + { x: -48, y: 0 }, + */ + ] + } + } + }, + + 'hexapod-single': '/img/game/npc/hexapod-single.png', /* 'hexapod': { @@ -41,7 +60,7 @@ let defaultAssets = { */ - + /* 'raiden': { type: 'spritesheet', @@ -58,7 +77,7 @@ let defaultAssets = { }, }, */ - 'blackMage': { + 'blackMage': { type: 'spritesheet', url: '/img/game/sheets/mage.png', frameTags: { @@ -137,7 +156,7 @@ let defaultAssets = { { x: -656, y: -656 }, ] }, - + player: { rate: 100, @@ -351,7 +370,7 @@ let defaultAssets = { ] }, - fire: { + fire: { frames: [ { x: -208, y: -544 }, { x: -224, y: -544 } diff --git a/mantra-game/plugins/coin/Coin.js b/mantra-game/plugins/coin/Coin.js new file mode 100644 index 00000000..2be7b8b0 --- /dev/null +++ b/mantra-game/plugins/coin/Coin.js @@ -0,0 +1,68 @@ +// Coin.js - Marak Squires 2024 +export default class Coin { + static id = 'coin'; + constructor(config = {}) { + this.id = Coin.id; + } + + init(game) { + this.game = game; + this.game.systemsManager.addSystem('coin', this); + } + + build(entityData = {}) { + if (typeof entityData.position === 'undefined') { + entityData.position = { x: 0, y: 0 }; + } + //let rules = this.sutra(); + return { + type: 'COIN', + size: { + width: 16, + height: 32 + }, + // equippable: true, + isSensor: true, + collisionStart: this.handleCoinCollision.bind(this), + // collectable: true, + //onCollect: true + + texture: { + sheet: "ayyo-coin", + sprite: 'coin', + }, + color: 0x00ff00, + // container: 'laby-container', + position: entityData.position + }; + } + + handleCoinCollision(a, b, pair, context) { + let coin = context.owner; + let other = context.target; + + if (other.type === 'COIN') { + return; + } + + // console.log("Coin Collision Detected!", context) + if (other.type === 'PLAYER') { + this.game.removeEntity(coin.id); + + this.game.updateEntity(other.id, { + score: other.score + 1 + }) + } + + } + create(entityData = {}) { + + if (typeof entityData.position === 'undefined') { + entityData.position = { x: 0, y: 0 }; + } + + // Create the Coin entity + const coin = game.createEntity(this.build(entityData)); + } + +} \ No newline at end of file diff --git a/mantra-game/plugins/crosswindow/CrossWindow.js b/mantra-game/plugins/crosswindow/CrossWindow.js index 1db3b561..edb892ca 100644 --- a/mantra-game/plugins/crosswindow/CrossWindow.js +++ b/mantra-game/plugins/crosswindow/CrossWindow.js @@ -6,9 +6,32 @@ import { CrossWindow as CW, CrossWindowDebugger } from 'crosswindow'; export default class CrossWindow { static id = 'crosswindow'; - constructor(game) { + constructor(options = {}) { this.id = CrossWindow.id; this.crosswindow = null; + this.debugger = null; + + this.config = {}; + this.config.debugger = { + 'showOtherWindows': false, + 'showWindowLegend': false, + 'showPositionLegend': false, + 'showOpenWindowButton': false, + 'showExamplesBar': false + }; + + if (typeof options.debugger === 'boolean') { + this.debugger = options.debugger; + } + if (typeof options.debugger === 'object') { + this.debugger = true; + // iterate through all options and set them on config + for (let key in options.debugger) { + console.log("in plugin setting config", key, options.debugger[key]) + this.config.debugger[key] = options.debugger[key]; + } + } + } init(game) { @@ -22,13 +45,19 @@ export default class CrossWindow { broadcastKeyboardEvents: true, }); - // Optionally initialize CrossWindow debugger - this.crossWindowDebugger = new CrossWindowDebugger(this.crosswindow, { - showOtherWindows: true, - showWindowLegend: true, - showWindowCount: true, - }); + if (this.debugger) { + console.log('crosswindow plugin config', this.config) + // Optionally initialize CrossWindow debugger + this.crossWindowDebugger = new CrossWindowDebugger(this.crosswindow, { + + showOtherWindows: this.config.debugger.showOtherWindows, + showWindowLegend: this.config.debugger.showWindowLegend, + showOpenWindowButton: this.config.debugger.showOpenWindowButton, + showExamplesBar: this.config.debugger.showExamplesBar, + }); + + } // Listen for Mantra and CrossWindow events this.setupListeners(); } @@ -134,7 +163,7 @@ export default class CrossWindow { // etc, all props, iterate? mantra helper? }; - + for (let p in entity) { if (typeof entity[p] !== 'function' && p !== 'graphics') { entityData[p] = entity[p]; diff --git a/mantra-game/plugins/entity/lib/createEntity.js b/mantra-game/plugins/entity/lib/createEntity.js index 6ecfc71e..7e59c274 100644 --- a/mantra-game/plugins/entity/lib/createEntity.js +++ b/mantra-game/plugins/entity/lib/createEntity.js @@ -53,7 +53,7 @@ export default function createEntity(config = {}, ignoreSetup = false) { // Remark: height, width, and depth are being replaced by size size: { width: 100, height: 100, depth: 16 }, lifetime: null, - maxSpeed: 9999, + maxSpeed: 3, isStatic: false, isSensor: false, restitution: 0, diff --git a/mantra-game/plugins/entity/lib/inflateEntity.js b/mantra-game/plugins/entity/lib/inflateEntity.js index 31160a31..6a620d99 100644 --- a/mantra-game/plugins/entity/lib/inflateEntity.js +++ b/mantra-game/plugins/entity/lib/inflateEntity.js @@ -56,11 +56,12 @@ export default function inflateEntity(entityData) { } } catch (err) { - console.warn('Failed to build remote entity by type:', type, err, 'using default build'); + // This will happen for any type that is not defined by an active plugin + // console.warn('Failed to build remote entity by type:', type, err, 'using default build'); defaultBuild(game, entityData) } - console.log('proceeding with typed data', entityData) + // console.log('proceeding with typed data', entityData) } else { defaultBuild(game, entityData); diff --git a/mantra-game/plugins/physics-matter/lib/onAfterUpdate.js b/mantra-game/plugins/physics-matter/lib/onAfterUpdate.js index ec6fb215..86a8c1e7 100644 --- a/mantra-game/plugins/physics-matter/lib/onAfterUpdate.js +++ b/mantra-game/plugins/physics-matter/lib/onAfterUpdate.js @@ -30,7 +30,7 @@ export default function onAfterUpdate(event) { // // Clamp max speed // - let maxSpeed = 100; // TODO: move to config + let maxSpeed = 4; // TODO: move to config if (entity.maxSpeed) { maxSpeed = entity.maxSpeed; }