Skip to content

Commit 6e08939

Browse files
committed
model: split object impl into seperate files
1 parent 5c677b4 commit 6e08939

File tree

18 files changed

+544
-381
lines changed

18 files changed

+544
-381
lines changed

twinhook/model/aabb.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "aabb.h"
2+
3+
#include <sstream>
4+
5+
#include "polygon.h"
6+
7+
vec2 aabb::com() const
8+
{
9+
return position + size / 2;
10+
}
11+
12+
std::shared_ptr<entity> aabb::translate(vec2 delta) const
13+
{
14+
auto c = std::make_shared<aabb>(*this);
15+
c->position += delta;
16+
return c;
17+
}
18+
19+
std::shared_ptr<entity> aabb::withVelocity(vec2 newVelocity) const
20+
{
21+
auto c = std::make_shared<aabb>(*this);
22+
c->velocity = newVelocity;
23+
return c;
24+
}
25+
26+
std::shared_ptr<aabb> aabb::boundingBox() const
27+
{
28+
return std::make_shared<aabb>(*this);
29+
}
30+
31+
float aabb::willExit(const entity& o) const
32+
{
33+
switch (o.type)
34+
{
35+
case AABB: {
36+
const auto& a = dynamic_cast<const aabb&>(o);
37+
float t = vec2::willExitAABB(a.position, position,
38+
a.size, size, a.velocity, velocity);
39+
return t;
40+
}
41+
default: return -1.f;
42+
}
43+
}
44+
45+
float aabb::willCollideWith(const entity& o) const
46+
{
47+
switch (o.type)
48+
{
49+
case AABB: {
50+
const auto& a = dynamic_cast<const aabb&>(o);
51+
return vec2::willCollideAABB(position, a.position,
52+
size, a.size, velocity, a.velocity);
53+
}
54+
case Polygon: {
55+
const auto& a = dynamic_cast<const polygon&>(o);
56+
const auto poly = this->toPolygon();
57+
return poly.willCollideWith(a);
58+
}
59+
default: return -1.f;
60+
}
61+
}
62+
63+
polygon aabb::toPolygon() const
64+
{
65+
return polygon{ {
66+
position, position + vec2(size.w, 0),
67+
position + size, position + vec2(0, size.h)
68+
}, velocity };
69+
}
70+
71+
std::ostream& aabb::serialize(std::ostream& os) const
72+
{
73+
os << "aabb p" << position << " v" << velocity << " s" << size;
74+
return os;
75+
}
76+

twinhook/model/aabb.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
#include <utility>
6+
7+
#include "entity.h"
8+
9+
class aabb : public entity
10+
{
11+
public:
12+
vec2 position;
13+
vec2 size;
14+
15+
aabb(const vec2& position = vec2(), const vec2& velocity = vec2(), const vec2& size = vec2(1, 1))
16+
: entity(AABB, velocity),
17+
position(position), size(size) {}
18+
19+
20+
aabb(const aabb& other)
21+
: entity{other}, position{other.position}, size{other.size} {}
22+
23+
vec2 com() const override;
24+
std::shared_ptr<entity> translate(vec2 delta) const override;
25+
std::shared_ptr<entity> withVelocity(vec2 newVelocity) const override;
26+
std::shared_ptr<aabb> boundingBox() const override;
27+
28+
float willExit(const entity& o) const override;
29+
float willCollideWith(const entity& o) const override;
30+
31+
polygon toPolygon() const;
32+
33+
std::ostream& serialize(std::ostream& os) const override;
34+
};

twinhook/model/circle.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "stdafx.h"
2+
#include "circle.h"
3+
#include "aabb.h"
4+
5+
vec2 circle::com() const
6+
{
7+
return center;
8+
}
9+
10+
std::shared_ptr<entity> circle::translate(vec2 delta) const
11+
{
12+
auto c = std::make_shared<circle>(*this);
13+
c->center += delta;
14+
return c;
15+
}
16+
17+
std::shared_ptr<entity> circle::withVelocity(vec2 newVelocity) const
18+
{
19+
auto c = std::make_shared<circle>(*this);
20+
c->velocity = newVelocity;
21+
return c;
22+
}
23+
24+
std::shared_ptr<aabb> circle::boundingBox() const
25+
{
26+
return std::make_shared<aabb>(center - vec2(radius),
27+
velocity, vec2(radius) * 2);
28+
}
29+
30+
float circle::willExit(const entity& o) const
31+
{
32+
switch (o.type)
33+
{
34+
case AABB: {
35+
// Use bounding box of circle
36+
return this->boundingBox()->willExit(o);
37+
}
38+
default: return -1.f;
39+
}
40+
}
41+
42+
float circle::willCollideWith(const entity& o) const
43+
{
44+
switch (o.type)
45+
{
46+
case Circle: {
47+
const auto& a = dynamic_cast<const circle&>(o);
48+
return vec2::willCollideCircle(center, a.center,
49+
radius, a.radius, velocity, a.velocity);
50+
}
51+
default: return -1.f;
52+
}
53+
}
54+
55+
std::ostream& circle::serialize(std::ostream& os) const
56+
{
57+
os << "circle c" << center << " v" << velocity << " r" << radius;
58+
return os;
59+
}

twinhook/model/circle.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
#include <utility>
6+
7+
#include "util/vec2.h"
8+
#include "entity.h"
9+
10+
class aabb;
11+
12+
class circle : public entity
13+
{
14+
public:
15+
vec2 center;
16+
float radius;
17+
18+
circle(const vec2& center = vec2(), const vec2& velocity = vec2(), float radius = 1)
19+
:entity(Circle, velocity), center(center), radius(radius) {}
20+
21+
vec2 com() const override;
22+
std::shared_ptr<entity> translate(vec2 delta) const override;
23+
std::shared_ptr<entity> withVelocity(vec2 newVelocity) const override;
24+
std::shared_ptr<aabb> boundingBox() const override;
25+
26+
float willExit(const entity& o) const override;
27+
float willCollideWith(const entity& o) const override;
28+
29+
std::ostream& serialize(std::ostream& os) const override;
30+
31+
};

twinhook/model/entity.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "entity.h"
2+
3+
std::shared_ptr<entity> entity::entityAtCollision(const entity& o) const
4+
{
5+
float t = this->willCollideWith(o);
6+
if (t < 0)
7+
return nullptr;
8+
return this->translate(t * this->velocity);
9+
}
10+
11+
std::ostream& operator<<(std::ostream& os, const entity& o)
12+
{
13+
return o.serialize(os);
14+
}
15+
16+
void swap(entity& lhs, entity& rhs) noexcept
17+
{
18+
using std::swap;
19+
swap(lhs.type, rhs.type);
20+
swap(lhs.velocity, rhs.velocity);
21+
}

twinhook/model/entity.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
#pragma once
3+
4+
#include <memory>
5+
#include <utility>
6+
#include <iosfwd>
7+
8+
#include "util/vec2.h"
9+
10+
class polygon;
11+
class aabb;
12+
13+
class entity
14+
{
15+
public:
16+
enum entity_type
17+
{
18+
AABB,
19+
Circle,
20+
Polygon
21+
};
22+
protected:
23+
entity(entity_type type, vec2 velocity = vec2())
24+
: type(type), velocity(velocity) {}
25+
26+
entity(const entity& other) = default;
27+
entity& operator=(const entity& other) = default;
28+
29+
public:
30+
entity_type type;
31+
vec2 velocity;
32+
33+
virtual ~entity() = default;
34+
35+
friend void swap(entity& lhs, entity& rhs) noexcept;
36+
37+
friend std::ostream& operator<<(std::ostream& os, const entity& o);
38+
virtual std::ostream& serialize(std::ostream& os) const = 0;
39+
40+
41+
/**
42+
* \brief Center of mass function
43+
* \return Center of mass of entity
44+
*/
45+
virtual vec2 com() const = 0;
46+
47+
virtual std::shared_ptr<aabb> boundingBox() const = 0;
48+
49+
/**
50+
* \brief Get translated entity
51+
* \param delta Translation delta
52+
* \return Entity translated by delta
53+
*/
54+
virtual std::shared_ptr<entity> translate(vec2 delta) const = 0;
55+
/**
56+
* \brief Get entity with new velocity
57+
* \param newVelocity The new velocity
58+
* \return Entity with new velocity
59+
*/
60+
virtual std::shared_ptr<entity> withVelocity(vec2 newVelocity) const = 0;
61+
62+
/**
63+
* \brief Predict time until collision between this entity and another
64+
* \param o The other entity
65+
* \return Result of the vector collision predictor (frames until collision)
66+
*/
67+
virtual float willCollideWith(const entity& o) const = 0;
68+
/**
69+
* \brief Predict time until entity leaves another's bounds
70+
* \param o The other entity
71+
* \return Result of the vector exit predictor (frames until exit)
72+
*/
73+
virtual float willExit(const entity& o) const = 0;
74+
75+
std::shared_ptr<entity> entityAtCollision(const entity& o) const;
76+
77+
};

twinhook/model/entiy.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
#include <utility>
6+
7+
#include "util/vec2.h"
8+
9+
class polygon;
10+
class aabb;
11+
12+
class entity
13+
{
14+
public:
15+
enum entity_type
16+
{
17+
AABB,
18+
Circle,
19+
Polygon
20+
};
21+
protected:
22+
entity(entity_type type, vec2 velocity = vec2())
23+
: type(type), velocity(std::move(velocity)) {}
24+
25+
entity& operator=(const entity& o) = default;
26+
27+
public:
28+
entity_type type;
29+
vec2 velocity;
30+
31+
virtual ~entity() = default;
32+
33+
/**
34+
* \brief Center of mass function
35+
* \return Center of mass of entity
36+
*/
37+
virtual vec2 com() const = 0;
38+
39+
virtual std::shared_ptr<aabb> boundingBox() const = 0;
40+
41+
/**
42+
* \brief Get translated entity
43+
* \param delta Translation delta
44+
* \return Entity translated by delta
45+
*/
46+
virtual std::shared_ptr<entity> translate(vec2 delta) const = 0;
47+
/**
48+
* \brief Get entity with new velocity
49+
* \param newVelocity The new velocity
50+
* \return Entity with new velocity
51+
*/
52+
virtual std::shared_ptr<entity> withVelocity(vec2 newVelocity) const = 0;
53+
54+
/**
55+
* \brief Predict time until collision between this entity and another
56+
* \param o The other entity
57+
* \return Result of the vector collision predictor (frames until collision)
58+
*/
59+
virtual float willCollideWith(const entity& o) const = 0;
60+
/**
61+
* \brief Predict time until entity leaves another's bounds
62+
* \param o The other entity
63+
* \return Result of the vector exit predictor (frames until exit)
64+
*/
65+
virtual float willExit(const entity& o) const = 0;
66+
67+
std::shared_ptr<entity> entityAtCollision(const entity& o) const;
68+
69+
virtual std::string serialize() const = 0;
70+
71+
friend void swap(entity& lhs, entity& rhs) noexcept;
72+
};

twinhook/model/game_object.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include "model/object.h"
4-
3+
#include <utility>
54

5+
#include "model/object.h"
66

77
class game_object
88
{
@@ -16,10 +16,7 @@ class game_object
1616
Enemy
1717
};
1818

19-
game_object(game_object_type type, std::shared_ptr<entity> obj) : type(type)
20-
{
21-
this->obj = obj;
22-
}
19+
game_object(game_object_type type, std::shared_ptr<entity> obj) : type(type), obj(std::move(obj)) {}
2320
public:
2421
game_object_type type;
2522
std::shared_ptr<entity> obj;

0 commit comments

Comments
 (0)