diff options
Diffstat (limited to 'src/systems/pondering.cpp')
-rw-r--r-- | src/systems/pondering.cpp | 88 |
1 files changed, 83 insertions, 5 deletions
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 02d5cfc..9115988 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp | |||
@@ -39,7 +39,7 @@ void PonderingSystem::tick(double dt) | |||
39 | ponderable.setVelocityY( | 39 | ponderable.setVelocityY( |
40 | ponderable.getVelocityY() + ponderable.getAccelY() * dt); | 40 | ponderable.getVelocityY() + ponderable.getAccelY() * dt); |
41 | 41 | ||
42 | if ((ponderable.getType() == PonderableComponent::Type::freefalling) | 42 | if ((ponderable.getBodyType() == PonderableComponent::BodyType::freefalling) |
43 | && (ponderable.getVelocityY() > TERMINAL_VELOCITY)) | 43 | && (ponderable.getVelocityY() > TERMINAL_VELOCITY)) |
44 | { | 44 | { |
45 | ponderable.setVelocityY(TERMINAL_VELOCITY); | 45 | ponderable.setVelocityY(TERMINAL_VELOCITY); |
@@ -58,7 +58,7 @@ void PonderingSystem::tick(double dt) | |||
58 | 58 | ||
59 | std::priority_queue<Collision> collisions; | 59 | std::priority_queue<Collision> collisions; |
60 | 60 | ||
61 | // Find collisions | 61 | // Find map collisions |
62 | for (id_type mapEntity : maps) | 62 | for (id_type mapEntity : maps) |
63 | { | 63 | { |
64 | auto& mappable = game_.getEntityManager(). | 64 | auto& mappable = game_.getEntityManager(). |
@@ -366,6 +366,51 @@ void PonderingSystem::tick(double dt) | |||
366 | } | 366 | } |
367 | } | 367 | } |
368 | 368 | ||
369 | // Find body collisions | ||
370 | for (id_type body : entities) | ||
371 | { | ||
372 | // Can't collide with self | ||
373 | if (body == entity) | ||
374 | { | ||
375 | continue; | ||
376 | } | ||
377 | |||
378 | // Make sure the body is collidable | ||
379 | auto& colliderPonderable = | ||
380 | game_.getEntityManager().getComponent<PonderableComponent>(body); | ||
381 | |||
382 | if (!colliderPonderable.isCollidable()) | ||
383 | { | ||
384 | continue; | ||
385 | } | ||
386 | |||
387 | // Test if the body was already colliding | ||
388 | auto& colliderTransformable = | ||
389 | game_.getEntityManager().getComponent<TransformableComponent>(body); | ||
390 | |||
391 | if ((oldRight > colliderTransformable.getX()) | ||
392 | && (oldX < colliderTransformable.getX() + colliderTransformable.getW()) | ||
393 | && (oldBottom > colliderTransformable.getY()) | ||
394 | && (oldY < colliderTransformable.getY() + colliderTransformable.getH())) | ||
395 | { | ||
396 | continue; | ||
397 | } | ||
398 | |||
399 | // Test if there is a new collision | ||
400 | if (!((newX + transformable.getW() > colliderTransformable.getX()) | ||
401 | && (newX < colliderTransformable.getX() + colliderTransformable.getW()) | ||
402 | && (newY + transformable.getH() > colliderTransformable.getY()) | ||
403 | && (newY < | ||
404 | colliderTransformable.getY() + colliderTransformable.getH()))) | ||
405 | { | ||
406 | continue; | ||
407 | } | ||
408 | |||
409 | // Process the collision | ||
410 | processBodyCollision(entity, body); | ||
411 | processBodyCollision(body, entity); | ||
412 | } | ||
413 | |||
369 | // Move | 414 | // Move |
370 | transformable.setX(newX); | 415 | transformable.setX(newX); |
371 | transformable.setY(newY); | 416 | transformable.setY(newY); |
@@ -399,13 +444,46 @@ void PonderingSystem::tick(double dt) | |||
399 | 444 | ||
400 | void PonderingSystem::initializeBody( | 445 | void PonderingSystem::initializeBody( |
401 | id_type entity, | 446 | id_type entity, |
402 | PonderableComponent::Type type) | 447 | PonderableComponent::BodyType bodyType, |
448 | PonderableComponent::ColliderType colliderType) | ||
403 | { | 449 | { |
404 | auto& ponderable = game_.getEntityManager(). | 450 | auto& ponderable = game_.getEntityManager(). |
405 | emplaceComponent<PonderableComponent>(entity, type); | 451 | emplaceComponent<PonderableComponent>(entity, bodyType, colliderType); |
406 | 452 | ||
407 | if (type == PonderableComponent::Type::freefalling) | 453 | if (bodyType == PonderableComponent::BodyType::freefalling) |
408 | { | 454 | { |
409 | ponderable.setAccelY(NORMAL_GRAVITY); | 455 | ponderable.setAccelY(NORMAL_GRAVITY); |
410 | } | 456 | } |
411 | } | 457 | } |
458 | |||
459 | void PonderingSystem::processBodyCollision(id_type body, id_type collider) | ||
460 | { | ||
461 | auto& bodyPonderable = game_.getEntityManager(). | ||
462 | getComponent<PonderableComponent>(body); | ||
463 | |||
464 | auto& colliderPonderable = game_.getEntityManager(). | ||
465 | getComponent<PonderableComponent>(collider); | ||
466 | |||
467 | switch (colliderPonderable.getColliderType()) | ||
468 | { | ||
469 | case PonderableComponent::ColliderType::event: | ||
470 | { | ||
471 | auto& callback = colliderPonderable. | ||
472 | getEventCallback(bodyPonderable.getColliderType()); | ||
473 | |||
474 | if (callback) | ||
475 | { | ||
476 | callback(game_); | ||
477 | } | ||
478 | |||
479 | break; | ||
480 | } | ||
481 | |||
482 | default: | ||
483 | { | ||
484 | // Do nothing. | ||
485 | |||
486 | break; | ||
487 | } | ||
488 | } | ||
489 | } | ||