Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/level.js
1036 views
1
/*globals define*/
2
define([
3
'object2d',
4
'geometry/geometry-factory',
5
'entities/physics-entity',
6
'utils'
7
], function( Object2D, GeometryFactory, PhysicsEntity, Utils ) {
8
'use strict';
9
10
function Level() {
11
Object2D.call( this );
12
13
this.entities = [];
14
this.shapes = [];
15
}
16
17
Level.prototype = new Object2D();
18
Level.prototype.constructor = Level;
19
20
Level.loadEntities = function( data ) {
21
var entities = [];
22
data.forEach(function() {});
23
return entities;
24
};
25
26
Level.loadPhysicsEntities = function( data ) {
27
var entities = [];
28
29
data.forEach(function( entityData ) {
30
entities.push( new PhysicsEntity( entityData ) );
31
});
32
33
return entities;
34
};
35
36
Level.loadBatchPhysicsEntities = function( data ) {
37
var entities = [];
38
/**
39
* WARNING: This only takes PolygonShapes, SetAsVector.
40
*/
41
data.forEach(function( batchData ) {
42
var properties = batchData.properties;
43
44
batchData.data.forEach(function( data ) {
45
// Create basic entityData, filling in all object fields so that they
46
// won't be overriden by defaults().
47
var entityData = {
48
data: data.data,
49
fixture: {
50
filter: {}
51
},
52
body: {
53
position: {
54
x: data.x || 0,
55
y: data.y || 0
56
},
57
angle: data.angle || 0
58
}
59
};
60
61
Utils.defaults( entityData, properties );
62
63
// Now fill in the fixture/filter/body objects, pretty much a deep clone.
64
if ( properties.fixture ) {
65
if ( properties.fixture.filter ) {
66
Utils.defaults( entityData.fixture.filter, properties.fixture.filter );
67
}
68
69
Utils.defaults( entityData.fixture, properties.fixture );
70
}
71
72
if ( properties.body ) {
73
Utils.defaults( entityData.body, properties.body );
74
}
75
76
// Hacky hack-hack for shapes.
77
if ( !entityData.shapes ) {
78
entityData.shapes = [];
79
}
80
81
// Add one pure black shape.
82
if ( !entityData.shapes.length ) {
83
entityData.shapes.push({
84
type: 'polygon',
85
fill: {
86
type: 'color',
87
alpha: 1
88
}
89
});
90
}
91
92
// We just copy over the vertices data from the main
93
// b2PolygonShape definition.
94
entityData.shapes[0].vertices = entityData.data;
95
96
entities.push( new PhysicsEntity( entityData ) );
97
});
98
});
99
100
return entities;
101
};
102
103
Level.prototype.fromJSON = function( json ) {
104
var jsonObject = JSON.parse( json );
105
106
var entities = [];
107
if ( jsonObject.entities ) {
108
entities = entities.concat( Level.loadEntities( jsonObject.entities ) );
109
}
110
111
if ( jsonObject.physicsEntities ) {
112
entities = entities.concat( Level.loadPhysicsEntities( jsonObject.physicsEntities ) );
113
}
114
115
if ( jsonObject.batchPhysicsEntities ) {
116
entities = entities.concat( Level.loadBatchPhysicsEntities( jsonObject.batchPhysicsEntities ) );
117
}
118
119
this.entities = entities;
120
};
121
122
return Level;
123
});
124
125