Path: blob/main/public/games/files/garbage-collector/js/level.js
1036 views
/*globals define*/1define([2'object2d',3'geometry/geometry-factory',4'entities/physics-entity',5'utils'6], function( Object2D, GeometryFactory, PhysicsEntity, Utils ) {7'use strict';89function Level() {10Object2D.call( this );1112this.entities = [];13this.shapes = [];14}1516Level.prototype = new Object2D();17Level.prototype.constructor = Level;1819Level.loadEntities = function( data ) {20var entities = [];21data.forEach(function() {});22return entities;23};2425Level.loadPhysicsEntities = function( data ) {26var entities = [];2728data.forEach(function( entityData ) {29entities.push( new PhysicsEntity( entityData ) );30});3132return entities;33};3435Level.loadBatchPhysicsEntities = function( data ) {36var entities = [];37/**38* WARNING: This only takes PolygonShapes, SetAsVector.39*/40data.forEach(function( batchData ) {41var properties = batchData.properties;4243batchData.data.forEach(function( data ) {44// Create basic entityData, filling in all object fields so that they45// won't be overriden by defaults().46var entityData = {47data: data.data,48fixture: {49filter: {}50},51body: {52position: {53x: data.x || 0,54y: data.y || 055},56angle: data.angle || 057}58};5960Utils.defaults( entityData, properties );6162// Now fill in the fixture/filter/body objects, pretty much a deep clone.63if ( properties.fixture ) {64if ( properties.fixture.filter ) {65Utils.defaults( entityData.fixture.filter, properties.fixture.filter );66}6768Utils.defaults( entityData.fixture, properties.fixture );69}7071if ( properties.body ) {72Utils.defaults( entityData.body, properties.body );73}7475// Hacky hack-hack for shapes.76if ( !entityData.shapes ) {77entityData.shapes = [];78}7980// Add one pure black shape.81if ( !entityData.shapes.length ) {82entityData.shapes.push({83type: 'polygon',84fill: {85type: 'color',86alpha: 187}88});89}9091// We just copy over the vertices data from the main92// b2PolygonShape definition.93entityData.shapes[0].vertices = entityData.data;9495entities.push( new PhysicsEntity( entityData ) );96});97});9899return entities;100};101102Level.prototype.fromJSON = function( json ) {103var jsonObject = JSON.parse( json );104105var entities = [];106if ( jsonObject.entities ) {107entities = entities.concat( Level.loadEntities( jsonObject.entities ) );108}109110if ( jsonObject.physicsEntities ) {111entities = entities.concat( Level.loadPhysicsEntities( jsonObject.physicsEntities ) );112}113114if ( jsonObject.batchPhysicsEntities ) {115entities = entities.concat( Level.loadBatchPhysicsEntities( jsonObject.batchPhysicsEntities ) );116}117118this.entities = entities;119};120121return Level;122});123124125