-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObj.js
95 lines (75 loc) · 2.17 KB
/
GameObj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
////////////////////////////////////////////////////////////////////////////////
// GameObj.js
//
// Data container for game object state in multiplayer asteroids game
//
// Copyright (C) 2013 Ben Murrell
////////////////////////////////////////////////////////////////////////////////
( function() {
var merge = require( './public/lib/merge.js' );
// Expose constructor
module.exports = GameObj;
////////////////////////////////////////////////////////////////////////////////
// Constructs a new GameObj with the given state merged over the default state
// and the given transmit mask merged with the default transmit mask.
//
// The transmit mask is used to determine what to send to clients vs what is
// only used by the server.
////////////////////////////////////////////////////////////////////////////////
function GameObj( aState, aTransmit ) {
var self = this;
// Default State
var defaultState = {
guid: -1,
name: 'unnamed',
type: '',
size: 64,
m: 5,
heading: 0,
pos: {
x: 32,
y: 32
},
v: {
x: 0,
y: 0
},
vMax: 0,
a: {
x: 0,
y: 0
},
friction: 0,
health: 100,
maxHealth: 100,
controlState: { up: 0, down: 0, left: 0, right: 0, shoot: 0 },
nextShot: 0,
numBullets: 0
};
// Things we should transmit to remote representations of this object
self.transmit = {
guid: true,
type: true,
size: true,
heading: true,
pos: true,
v: true,
a: true,
friction: true,
health: true,
maxHealth: true
};
// Merge state with default state
merge( defaultState, aState );
// Move state into this object
merge( self, defaultState );
// Merge in transmission info
merge( self.transmit, aTransmit );
// Error check transmission info
for( var key in self.transmit ) {
if( self[key] === undefined ) {
console.warn( 'New object wants to transmit "' + key + '" but doesn\'t have it in state.' );
}
}
} // End GameObj()
})();