22 |
#include "actors.hh" |
#include "actors.hh" |
23 |
#include <algorithm> |
#include <algorithm> |
24 |
#include <cassert> |
#include <cassert> |
25 |
|
#include <map> |
26 |
|
|
27 |
using namespace world::laser; |
using namespace world::laser; |
28 |
using namespace world; |
using namespace world; |
29 |
|
using std::map; |
30 |
|
|
31 |
//====================================================================== |
//====================================================================== |
32 |
// TYPES |
// TYPES |
40 |
public: |
public: |
41 |
static void emit_from(GridPos p, Direction d); |
static void emit_from(GridPos p, Direction d); |
42 |
static void kill_all(); |
static void kill_all(); |
43 |
|
static void all_emitted(); |
44 |
|
|
45 |
// LaserEmitter interface |
// LaserEmitter interface |
46 |
DirectionBits emission_directions() const { return directions; } |
DirectionBits emission_directions() const { return directions; } |
64 |
|
|
65 |
// Variables |
// Variables |
66 |
DirectionBits directions; |
DirectionBits directions; |
67 |
static vector<void*> instances; |
|
68 |
|
static vector<LaserBeam*> instances; |
69 |
|
static map<GridPos, int> old_laser_positions; |
70 |
}; |
}; |
71 |
|
|
72 |
//---------------------------------------- |
//---------------------------------------- |
224 |
// stones and mirrors. |
// stones and mirrors. |
225 |
//---------------------------------------- |
//---------------------------------------- |
226 |
|
|
227 |
vector<void*> LaserBeam::instances; |
vector<LaserBeam*> LaserBeam::instances; |
228 |
|
map<GridPos, int> LaserBeam::old_laser_positions; |
229 |
|
|
230 |
void LaserBeam::init_model() |
void LaserBeam::init_model() |
231 |
{ |
{ |
300 |
|
|
301 |
void LaserBeam::kill_all() |
void LaserBeam::kill_all() |
302 |
{ |
{ |
303 |
|
assert(old_laser_positions.empty()); |
304 |
|
|
305 |
while (!instances.empty()) |
while (!instances.empty()) |
306 |
{ |
{ |
307 |
LaserBeam *lb = (LaserBeam*) instances[0]; |
LaserBeam *lb = instances[0]; |
308 |
world::KillItem(lb->get_pos()); |
GridPos pos = lb->get_pos(); |
309 |
|
|
310 |
|
old_laser_positions[pos] = static_cast<int>(lb->directions); |
311 |
|
world::KillItem(pos); |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
void LaserBeam::all_emitted() |
316 |
|
{ |
317 |
|
vector<LaserBeam*>::const_iterator end = instances.end(); |
318 |
|
map<GridPos, int>::iterator none = old_laser_positions.end(); |
319 |
|
|
320 |
|
double x = 0, y = 0; |
321 |
|
int count = 0; |
322 |
|
|
323 |
|
for (vector<LaserBeam*>::const_iterator i = instances.begin(); i != end; ++i) { |
324 |
|
LaserBeam *lb = *i; |
325 |
|
GridPos pos = lb->get_pos(); |
326 |
|
map<GridPos, int>::iterator found = old_laser_positions.find(pos); |
327 |
|
|
328 |
|
if (found != none) { |
329 |
|
// a beam was at the current position (during last kill_all()) |
330 |
|
DirectionBits old_dir = static_cast<DirectionBits>(found->second); |
331 |
|
|
332 |
|
if ((old_dir&lb->directions) != lb->directions) { |
333 |
|
// a beam has been added here |
334 |
|
x += pos.x; |
335 |
|
y += pos.y; |
336 |
|
++count; |
337 |
|
} |
338 |
|
} |
339 |
|
else { |
340 |
|
// store newly created LaserBeams |
341 |
|
x += pos.x; |
342 |
|
y += pos.y; |
343 |
|
++count; |
344 |
|
} |
345 |
|
} |
346 |
|
|
347 |
|
if (count) { |
348 |
|
sound::PlaySound("st-laser", px::V2(x/count+.5, y/count+.5)); |
349 |
} |
} |
350 |
|
|
351 |
|
old_laser_positions.clear(); |
352 |
} |
} |
353 |
|
|
354 |
void LaserBeam::dispose() |
void LaserBeam::dispose() |
388 |
} |
} |
389 |
} |
} |
390 |
|
|
391 |
void LaserStone::notify_onoff(bool on) |
void LaserStone::notify_onoff(bool /*on*/) |
392 |
{ |
{ |
393 |
RecalcLight(); |
RecalcLight(); |
|
if (on) |
|
|
sound::PlaySound("st-laseron"); |
|
394 |
} |
} |
395 |
|
|
396 |
void LaserStone::emit_light() |
void LaserStone::emit_light() |
756 |
LaserBeam::kill_all(); |
LaserBeam::kill_all(); |
757 |
LaserStone::reemit_all(); |
LaserStone::reemit_all(); |
758 |
PhotoCell::notify_finish(); |
PhotoCell::notify_finish(); |
759 |
|
LaserBeam::all_emitted(); |
760 |
light_recalc_scheduled = false; |
light_recalc_scheduled = false; |
761 |
} |
} |
762 |
} |
} |