Skip to content

Commit

Permalink
Adds Coin plugin
Browse files Browse the repository at this point in the history
`Entity` - Reduces default `maxSpeed` from 9999 -> 3
`CrossWindow` - Updates debugger configuration API
`Game` - Renaming `randomColor()` -> `getRandomColor()`
  • Loading branch information
Marak committed Mar 9, 2024
1 parent 996247d commit afc37b9
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 18 deletions.
Binary file added mantra-client/public/img/game/env/coin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions mantra-game/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion mantra-game/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -179,6 +180,7 @@ let plugins = {
Bomb,
Boomerang,
Bullet,
Coin,
Flame,
Sword,
// UI Objects
Expand Down
29 changes: 24 additions & 5 deletions mantra-game/plugins/client/defaultAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -41,7 +60,7 @@ let defaultAssets = {
*/



/*
'raiden': {
type: 'spritesheet',
Expand All @@ -58,7 +77,7 @@ let defaultAssets = {
},
},
*/
'blackMage': {
'blackMage': {
type: 'spritesheet',
url: '/img/game/sheets/mage.png',
frameTags: {
Expand Down Expand Up @@ -137,7 +156,7 @@ let defaultAssets = {
{ x: -656, y: -656 },
]
},


player: {
rate: 100,
Expand Down Expand Up @@ -351,7 +370,7 @@ let defaultAssets = {
]
},

fire: {
fire: {
frames: [
{ x: -208, y: -544 },
{ x: -224, y: -544 }
Expand Down
68 changes: 68 additions & 0 deletions mantra-game/plugins/coin/Coin.js
Original file line number Diff line number Diff line change
@@ -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));
}

}
45 changes: 37 additions & 8 deletions mantra-game/plugins/crosswindow/CrossWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion mantra-game/plugins/entity/lib/createEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions mantra-game/plugins/entity/lib/inflateEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion mantra-game/plugins/physics-matter/lib/onAfterUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit afc37b9

Please sign in to comment.