In this recipe, we'll create a Level class which will be used to render the level and provide an API to the bundary map.
Follow these steps to create a Level class:
/* * Level class should have no knowledge * of the Actor or HealthBar classes to * keep it decoupled */ function Level(config){ this.controller = config.controller; this.x = config.x; this.y = config.y; this.leftBounds = config.leftBounds; this.rightBounds = config.rightBounds; this.boundsData = null; this.GRAVITY = 3; // px / second^2 this.MID_RGB_COMPONENT_VALUE = 128; this.LEVEL_WIDTH = 6944; this.setBoundsData(); }
Define the
setBoundsData()
method which extracts the zone data from the boundary map image:Level.prototype.setBoundsData = function(){ var controller = this.controller; var canvas = controller.view.canvas; var context = controller.view.context; canvas.width = 6944; context.drawImage(controller...