1
+ /**
2
+ * Enum for possible Actions
3
+ *
4
+ * @readonly
5
+ * @enum {int}
6
+ * @author Christian Vogt <[email protected] >
7
+ */
8
+ var ACTIONS = {
9
+ up : 0 ,
10
+ right : 1 ,
11
+ down : 2 ,
12
+ left : 3
13
+ } ;
14
+
15
+ /**
16
+ * Defines the "randbedingungen"
17
+ *
18
+ * @constructor
19
+ * @param {int } _height The height of the environment (in pixel-like values)
20
+ * @param {int } _width The width of the environment (in pixel-like values)
21
+ * @property {int } height The height of the environment (in pixel-like values)
22
+ * @property {int } width The width of the environment (in pixel-like values)
23
+ * @property {Array } obstacles Array containing the obstacles of this environment
24
+ * @property {Array } players Array containing the players in this environment
25
+ * @author Christian Vogt <[email protected] >
26
+ */
27
+ function Environment ( _height , _width ) {
28
+ this . height = _height ;
29
+ this . width = _width ;
30
+ this . obstacles = [ ] ;
31
+ this . players = [ ] ;
32
+
33
+ /**
34
+ * Determine the resulting Position if moved from one position to another and return it.
35
+ *
36
+ * @param {Position } _currentPosition the current Position
37
+ * @param {Position } _desiredPosition the desired Position of the Player
38
+ * @returns {Position } the Position actually reached
39
+ */
40
+ this . getResultingPosition = function ( _currentPosition , _desiredPosition ) {
41
+
42
+ // out of bounds check
43
+ if ( _desiredPosition . posx < 0 || _desiredPosition . posy < 0 || _desiredPosition . posx >= this . width || _desiredPosition . posy >= this . height ) {
44
+ // console.log('out of bound');
45
+ return _currentPosition ;
46
+ }
47
+
48
+ // obstacle check
49
+ for ( var i = 0 ; i < this . obstacles . length ; i ++ ) {
50
+ if ( this . obstacles [ i ] . position . equals ( _desiredPosition ) ) return _currentPosition ;
51
+ }
52
+
53
+ // other player check
54
+ for ( var j = 0 ; j < this . players . length ; j ++ ) {
55
+ if ( this . players [ j ] . position . equals ( _desiredPosition ) ) return _currentPosition ;
56
+ }
57
+
58
+ // all right, go for it
59
+ return _desiredPosition ;
60
+ } ;
61
+
62
+ /**
63
+ * Draws the current environment in the specified `canvas` with a framerate of `framesPerSecond` and a scaling factor `scale`.
64
+ *
65
+ * @param {window[canvas] } _canvas The DomElement this environment shall be drawed into
66
+ * @param {int } _framesPerSecond Times to draw per second (fps)
67
+ * @param {int } _scale Factor this canvas scales the pixel-like size of the environment
68
+ */
69
+ this . draw = function ( _canvas , _framesPerSecond , _scale ) {
70
+ var self = this ;
71
+ var canvas = _canvas ;
72
+ var framesPerSecond = _framesPerSecond ;
73
+ var scale = _scale ;
74
+ var color = {
75
+ background : "#000" ,
76
+ hunter : "#fff" ,
77
+ obstacle : "FA5858" ,
78
+ victim : "D7DF01"
79
+ } ;
80
+ if ( ! scale ) {
81
+ scale = 50 ;
82
+ }
83
+ if ( ! framesPerSecond ) {
84
+ framesPerSecond = 10 ;
85
+ }
86
+ canvas . width = this . width * scale ;
87
+ canvas . height = this . height * scale ;
88
+
89
+ // draw ground layer
90
+ ctx = canvas . getContext ( "2d" ) ;
91
+ ctx . fillStyle = color . background ;
92
+ ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
93
+
94
+ // get hurdle position and draw
95
+ this . obstacles . forEach ( function ( obstacle ) {
96
+ ctx . fillStyle = color . obstacle ; //red
97
+ ctx . fillRect ( obstacle . position . posx * scale , obstacle . position . posy * scale , scale , scale ) ;
98
+ } ) ;
99
+
100
+ // get player position and draw
101
+ this . players . forEach ( function ( player ) {
102
+ if ( player instanceof Hunter ) {
103
+ ctx . fillStyle = color . hunter ; // Hunter
104
+ } else if ( player instanceof Victim ) {
105
+ ctx . fillStyle = color . victim ; // Victim
106
+ }
107
+ ctx . fillRect ( player . position . posx * scale , player . position . posy * scale , scale , scale ) ;
108
+ } ) ;
109
+
110
+ setTimeout ( function ( ) {
111
+ self . draw ( canvas , framesPerSecond , scale ) ;
112
+ } , Math . floor ( 1000 / framesPerSecond ) ) ;
113
+ } ;
114
+ }
115
+
116
+ /**
117
+ * Position
118
+ *
119
+ * @constructor
120
+ * @param {int } _posx the x-coordinate
121
+ * @param {int } _posy the y-coordinate
122
+ * @property {int } posx the x-coordinate
123
+ * @property {int } posy the y-coordinate
124
+ */
125
+ function Position ( _posx , _posy ) {
126
+ this . posx = _posx ;
127
+ this . posy = _posy ;
128
+
129
+ /**
130
+ * Moves up (y+1)
131
+ *
132
+ * @returns {Position } The resulting position
133
+ */
134
+ this . up = function ( ) {
135
+ return new Position ( this . posx , this . posy - 1 ) ;
136
+ } ;
137
+
138
+ /**
139
+ * Moves right (x+1)
140
+ *
141
+ * @returns {Position } The resulting position
142
+ */
143
+ this . right = function ( ) {
144
+ return new Position ( this . posx + 1 , this . posy ) ;
145
+ } ;
146
+
147
+ /**
148
+ * Moves down (y+1)
149
+ *
150
+ * @returns {Position } The resulting position
151
+ */
152
+ this . down = function ( ) {
153
+ return new Position ( this . posx , this . posy + 1 ) ;
154
+ } ;
155
+
156
+ /**
157
+ * Moves left (x-1)
158
+ *
159
+ * @returns {Position } The resulting position
160
+ */
161
+ this . left = function ( ) {
162
+ return new Position ( this . posx - 1 , this . posy ) ;
163
+ } ;
164
+
165
+ /**
166
+ * Determine if `otherposition` is the same as this position
167
+ *
168
+ * @param {Position } _otherposition The other Position
169
+ * @returns {Boolean } If this position equals the `otherposition`
170
+ */
171
+ this . equals = function ( _otherposition ) {
172
+ if ( ! _otherposition instanceof Position ) {
173
+ return false ;
174
+ } else if ( _otherposition . posx != this . posx ) {
175
+ return false ;
176
+ } else if ( _otherposition . posy != this . posy ) {
177
+ return false ;
178
+ }
179
+ return true ;
180
+ } ;
181
+
182
+ /**
183
+ * Hash this Position to string
184
+ *
185
+ * @returns {String }
186
+ */
187
+ this . hash = function ( ) {
188
+ return ( btoa ( this . posx ) + btoa ( this . posy ) ) ;
189
+ } ;
190
+
191
+ /**
192
+ * Determine the distance to another Position
193
+ *
194
+ * @param {Position } _otherposition The other Position
195
+ * @returns {int } The distance to the other Position
196
+ */
197
+ this . distance = function ( _otherposition ) {
198
+ return Math . abs ( _otherposition . posx - this . posx ) + Math . abs ( _otherposition . posy - this . posy ) ;
199
+ } ;
200
+
201
+ /**
202
+ * Determine the direction to another Position with a max. sight range of `maxDistance`
203
+ *
204
+ * @param {Position } _otherposition The position to get the direction to
205
+ * @param {int } maxDistance The max. distance that is allowed to be seen
206
+ */
207
+ this . directionTo = function ( _otherposition , maxDistance ) {
208
+ if ( maxDistance && this . distance ( _otherposition ) > maxDistance ) {
209
+ return null ;
210
+ }
211
+ if ( this . equals ( _otherposition ) ) {
212
+ return null ;
213
+ }
214
+ var candidates = [ ] ;
215
+ if ( this . posx < _otherposition . posx ) { // ich bin weiter links
216
+ candidates . push ( ACTIONS . right ) ;
217
+ } else if ( this . posx > _otherposition . posx ) {
218
+ candidates . push ( ACTIONS . left ) ;
219
+ }
220
+ if ( this . posy < _otherposition . posy ) { // ich bin weiter oben
221
+ candidates . push ( ACTIONS . down ) ;
222
+ } else if ( this . posy > _otherposition . posy ) {
223
+ candidates . push ( ACTIONS . up ) ;
224
+ }
225
+ // choose random from candidates
226
+ return candidates [ Math . floor ( Math . random ( ) * candidates . length ) ] ;
227
+ } ;
228
+
229
+ /**
230
+ * Returns a string of this position
231
+ *
232
+ * @returns {String } The string value
233
+ */
234
+ this . toString = function ( ) {
235
+ return JSON . stringify ( this ) ;
236
+ } ;
237
+ }
238
+
239
+ /**
240
+ * Obstacle
241
+ *
242
+ * Obstacle that cannot be crossed by a player
243
+ *
244
+ * @constructor
245
+ * @param {Position } _position The Position this obstacle uses
246
+ * @property {Position } position The Position this obstacle uses
247
+ */
248
+ function Obstacle ( _position ) {
249
+ this . position = _position ;
250
+ }
0 commit comments