Skip to content
Coderman64 edited this page Jan 11, 2022 · 8 revisions

System Design > (Items)

Item System Design

Motobug Engine has an item system that allows for lots of customization and the addition of all types of stage objects/stage gimmicks to your games! There are several items that are built into the engine, which can be seen in engine/items.js (link). User-created items go in res/user_items.js. This file is overwritten by Motobug Studio with the contents of all code blocks in an .mbitm file when exporting a project.

A Basic Item:

var basicItem = function(x,y,w,h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.destroy = false;
    this.disable = false;
    this.targetable = false;
    this.solid = false;
    this.platform = false;
    this.hrid = "spring";
    this.hit = function(char){}
    this.draw = function(ctx,camx,camy){}
    this.phys = function(){}
    this.reset = function(){}
}

Key

* - required

Properties

x *

the x-position of the item

y *

the y-position of the item

w *

the width of the item

h *

the height of the item

destroy *

If set to true, Motobug will remove this item from the item queue and discard it.

disable

temporarily disables the item. This will prevent the item from being drawn or interacted with. However, the reset function will still be run when the level is restarted.

targetable

If set to true, characters with a homing attack will be able to target this item.

solid

If set to true, the character will collide with the item as if it were a solid object. The object is assumed to be the bounding box (x,y,w,h).

platform

If set to true, the object will have collision, like with solid, however, the object can only be collided with from the top. This allows you to jump through the bottom of platforms.

hrid (unused)

Short for "human-readable ID". A short string describing the item, used for debugging. It currently is unused, but should be included for future compatibility.

Functions

this.hit = function(char){}

Run whenever your player's collider overlaps with the item's (x,y,w,h) bounding box.

  • char - the player object that hit this item.

this.draw = function(ctx,camx,camy){}

Run whenever the item's bounding box (x,y,w,h) is in view.

  • ctx - is the main CanvasRenderingContext2D object
  • camx, camy - the negative x- and y-position of the camera. A screen-space position can be created with ([world x coord]+camx,[world y coord]+camy)

this.phys = function(){}

Run every frame, regardless if the item is or is not in view. Use this sparingly, as it can cause the most performance impact.

this.reset = function(){}

Run when the game is "reset", i.e. when Sonic respawns. This is done to prevent reloading the entire level over the network. Usually, this involves resetting the item to its original position and state.