2016-09-27 16:23:58 +00:00
|
|
|
#include "missile.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "state.hpp"
|
|
|
|
#include "player.hpp"
|
2016-09-27 18:13:09 +00:00
|
|
|
#include "ship.hpp"
|
|
|
|
#include "planet.hpp"
|
|
|
|
#include "trace.hpp"
|
2016-09-27 16:23:58 +00:00
|
|
|
|
|
|
|
#include <glm/gtx/norm.hpp>
|
|
|
|
|
|
|
|
namespace game {
|
2016-10-02 21:49:40 +00:00
|
|
|
Missile::Missile(size_t id, Player *player, const glm::vec2 &pos, float angle, float speed)
|
2016-10-03 11:37:49 +00:00
|
|
|
: Object(id, pos)
|
2016-10-02 21:49:40 +00:00
|
|
|
, player(player)
|
2016-09-27 16:23:58 +00:00
|
|
|
{
|
|
|
|
velocity = speed * glm::vec2(std::sin(angle), std::cos(angle));
|
|
|
|
|
|
|
|
//std::cout << "spawned missile for player " << playerId << " @ ("
|
|
|
|
// << pos.x << ", " << pos.y << ") speed = " << speed << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
Missile::~Missile()
|
|
|
|
{
|
2016-09-28 15:38:32 +00:00
|
|
|
//std::cout<<"~Missile" << std::endl;
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Missile::Event Missile::advance(const game::State *state, float dt)
|
|
|
|
{
|
|
|
|
glm::vec2 gravityForce = glm::vec2(0.0, 0.0);
|
|
|
|
|
|
|
|
for (const Player *other : state->players) {
|
2016-09-27 18:13:09 +00:00
|
|
|
if (other->ship != nullptr && other != player) {
|
2016-09-27 16:23:58 +00:00
|
|
|
glm::vec2 diff = other->ship->position - position;
|
|
|
|
float dist = glm::length(diff);
|
|
|
|
if (dist <= other->ship->radius) {
|
|
|
|
// TODO: collect all hits and return the first one only
|
|
|
|
// TODO: find exact hit position!
|
2016-10-02 19:06:02 +00:00
|
|
|
return Missile::Event(position, velocity, player->id, other->id);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get forces
|
|
|
|
for (const Planet *planet : state->planets) {
|
|
|
|
glm::vec2 diff = planet->position - position;
|
|
|
|
float dist = glm::length(diff);
|
|
|
|
if (dist <= planet->radius) {
|
|
|
|
// TODO: collect all hits and return the first one only
|
|
|
|
// TODO: find exact hit position!
|
2016-10-02 19:06:02 +00:00
|
|
|
return Missile::Event(position, velocity, planet->id);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dist *= 20.0;
|
|
|
|
if (dist > 0.001) {
|
|
|
|
float G = 0.1;
|
|
|
|
gravityForce += G * diff / (1.0f + dist*dist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple (and bad) euler integration.
|
|
|
|
position += 0.5f * gravityForce*dt*dt + velocity*dt;
|
|
|
|
velocity += gravityForce * dt;
|
|
|
|
|
|
|
|
//const float thr = 0.01;
|
|
|
|
//float s = glm::length(velocity);
|
|
|
|
//if (s > thr) {
|
|
|
|
// velocity = glm::normalize(velocity) * thr;
|
|
|
|
//}
|
|
|
|
|
|
|
|
glm::vec2 nextPos = position + velocity;
|
|
|
|
|
|
|
|
// TODO: collision checks
|
|
|
|
position = nextPos;
|
|
|
|
|
|
|
|
// check if distance to center of the universe is getting too big
|
|
|
|
float distToCenter = glm::length(position);
|
|
|
|
if (distToCenter > state->maxMissileDistance()) {
|
2016-10-02 19:06:02 +00:00
|
|
|
return Missile::Event(position, velocity, Hit::BorderOfUniverse);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 19:06:02 +00:00
|
|
|
return Missile::Event(position, velocity);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
}
|