-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimePilot.Bullet.js
151 lines (133 loc) · 4.18 KB
/
TimePilot.Bullet.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* global define */
define("TimePilot.Bullet", [
"TimePilot.CONSTANTS",
"TimePilot.dataStore",
"TimePilot.userOptions",
"engine/helpers"
], function (
CONSTS,
dataStore,
userOptions,
helpers
) {
/**
* Creates a bullet to add to render.
* @constructor
* @param {Number} originX - Spawning location on the X axis.
* @param {Number} originY - Spawning location on the Y axis.
* @param {Number} heading - Start heading (usually towards the player).
* @returns {Bullet Instance}
*/
var Bullet = function (originX, originY, heading, size, velocity, color) {
this._gameArena = dataStore._gameArena;
this._data = {};
this._data.posX = originX;
this._data.posY = originY;
this._data.heading = heading;
this._data.size = size;
this._data.velocity = velocity;
this._data.color = color;
this.removeMe = false;
};
Bullet.prototype = {
/**
* Get data for the player.
* @method
* @param {String} [key] Key to return when requesting data. If no key is provided, it returns the object.
* @returns {Object}
*/
getData: function (key) {
if (!key) {
return this._data;
} else if (this._data.hasOwnProperty(key)) {
return this._data[key];
}
return;
},
/**
* Set data in the player's data object.
* @method
* @param {String} key - Key from _data object
* @param {Multi} value - Value to be set onto the key from the _data object.
* @returns {Boolean} Success response.
*/
setData: function (key, value) {
if (this._data[key] !== undefined) {
this._data[key] = value;
return (this._data[key] === value);
} else {
return false;
}
},
/**
* The current level.
* @type {Number}
*/
_level: 1,
/**
* Set current level.
* @method
* @param {Number} level - Level number to be set.
* @returns {Boolean}
*/
setLevel: function (level) {
this._level = level;
return (this._level === level);
},
/**
* Detect if the entity has left a given radius of the player.
* @method
* @protected
*/
_checkInArena: function () {
if (this.removeMe) {
return;
}
this.removeMe = helpers.detectAreaExit({
posX: (this._data.size / 2),
posY: (this._data.size / 2)
}, {
posX: this._data.posX,
posY: this._data.posY
},
CONSTS.limits.despawnRadius
);
},
/**
* Reposition the entity.
* @method
*/
reposition: function () {
var velocity = this._data.velocity,
heading = this._data.heading;
this._data.posX += helpers.float(Math.sin(heading * (Math.PI / 180)) * velocity);
this._data.posY -= helpers.float(Math.cos(heading * (Math.PI / 180)) * velocity);
this._checkInArena();
},
/**
* Render the entity.
* @method
*/
render: function () {
var size = this._data.size,
color = this._data.color,
context = this._gameArena.getContext();
context.fillStyle = color;
context.fillRect(
this._data.posX - (size / 2),
this._data.posY - (size / 2),
size, size
);
if (userOptions.enableDebug && userOptions.debug.showHitboxes) {
this._gameArena.drawCircle(
this._data.posX,
this._data.posY,
this._data.size, {
strokeColor: "#0F0"
}
);
}
}
};
return Bullet;
});