This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActorPropertyPhysics.cpp
183 lines (149 loc) · 5.84 KB
/
ActorPropertyPhysics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Moonfall Copyright (C) 2008 Alex Yatskov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Pch.h"
#include "ActorPropertyPhysics.h"
#include "ActorManager.h"
#include "Actor.h"
const ActorShapeType ActorPropertyPhysics::s_collisionShapes[ACTOR_SHAPE_TYPE_COLLISION_COUNT] =
{
ACTOR_SHAPE_TYPE_COLLISION_SOLID,
ACTOR_SHAPE_TYPE_COLLISION_INTERACT
};
ActorPropertyPhysics::ActorPropertyPhysics(Actor* owner) :
ActorPropertyTyped<ACTOR_PROPERTY_TYPE_PHYSICS>(owner)
{
}
void ActorPropertyPhysics::Update(float elapsed)
{
Actor* const actor = GetOwner();
bool collisionShapesPresent = false;
for (int i = 0; i < ARRAY_SIZE(s_collisionShapes); ++i)
{
const ActorShapeType type = s_collisionShapes[i];
if (actor->IsShapeEnabled(type))
{
Vector2i position;
Vector2i size;
int data = 0;
actor->GetShape(type, &data, &position, &size);
collisionShapesPresent |= (data != 0);
}
m_collisionState[type].collisions.clear();
}
const Vector2f newPosition = actor->GetPositionSoft() + GetVelocity() * elapsed;
if (!collisionShapesPresent)
{
actor->SetPositionSoft(newPosition);
return;
}
std::vector<boost::weak_ptr<Actor> > actors;
actor->GetOwner()->EnumerateActors(&actors, actor->GetPosition());
actor->SetPositionSoft(newPosition);
for (int i = 0; i < ARRAY_SIZE(s_collisionShapes); ++i)
{
CollideAgainstShape(actors, s_collisionShapes[i]);
}
}
void ActorPropertyPhysics::CollideAgainstShape(const std::vector<boost::weak_ptr<Actor> >& actors, ActorShapeType typeAttacker)
{
Actor* const actorAttacker = GetOwner();
Vector2i attackerPosition;
Vector2i attackerSize;
int attackerData = 0;
actorAttacker->GetShape(typeAttacker, &attackerData, &attackerPosition, &attackerSize);
if (!actorAttacker->IsShapeEnabled(typeAttacker) || attackerData == 0)
{
return;
}
for (std::vector<boost::weak_ptr<Actor> >::const_iterator iter = actors.begin(); iter != actors.end(); ++iter)
{
const boost::shared_ptr<Actor> actorDefender = iter->lock();
if (!actorDefender || !actorDefender->HasProperty(ACTOR_PROPERTY_TYPE_PHYSICS) || actorDefender->GetId() == actorAttacker->GetId())
{
continue;
}
for (int i = 0; i < ARRAY_SIZE(s_collisionShapes); ++i)
{
const ActorShapeType typeDefender = s_collisionShapes[i];
Vector2i defenderPosition;
Vector2i defenderSize;
int defenderData = 0;
actorDefender->GetShape(typeDefender, &defenderData, &defenderPosition, &defenderSize);
if (!actorDefender->IsShapeEnabled(typeDefender) || (attackerData & BIT(typeDefender)) == 0)
{
continue;
}
const Rect4i rectAttacker(
actorAttacker->GetPosition().x + attackerPosition.x,
actorAttacker->GetPosition().y + attackerPosition.y,
actorAttacker->GetPosition().x + attackerPosition.x + attackerSize.x,
actorAttacker->GetPosition().y + attackerPosition.y + attackerSize.y
);
const Rect4i rectDefender(
actorDefender->GetPosition().x + defenderPosition.x,
actorDefender->GetPosition().y + defenderPosition.y,
actorDefender->GetPosition().x + defenderPosition.x + defenderSize.x,
actorDefender->GetPosition().y + defenderPosition.y + defenderSize.y
);
if (!rectAttacker.Intersects(rectDefender))
{
continue;
}
if (typeAttacker != ACTOR_SHAPE_TYPE_COLLISION_SOLID || typeDefender != ACTOR_SHAPE_TYPE_COLLISION_SOLID)
{
m_collisionState[typeAttacker].collisions.push_back(actorDefender);
continue;
}
const Rect4i intersection = rectAttacker.Intersection(rectDefender);
Vector2i displacement(0, 0);
if (GetVelocity().x > 0)
{
displacement.x = -intersection.GetWidth();
}
else if (GetVelocity().x < 0)
{
displacement.x = intersection.GetWidth();
}
if (GetVelocity().y > 0)
{
displacement.y = -intersection.GetHeight();
}
else if (GetVelocity().y < 0)
{
displacement.y = intersection.GetHeight();
}
actorAttacker->SetPosition(actorAttacker->GetPosition() + displacement);
}
}
}
void ActorPropertyPhysics::SetVelocity(const Vector2f& velocity)
{
m_velocity = velocity;
}
Vector2f ActorPropertyPhysics::GetVelocity() const
{
return m_velocity;
}
void ActorPropertyPhysics::QueryShapeCollisions(ActorShapeType type, std::vector<boost::weak_ptr<Actor> >* actors) const
{
ASSERT(type < ACTOR_SHAPE_TYPE_COUNT);
const CollisionState& state = m_collisionState[type];
for (std::vector<boost::weak_ptr<Actor> >::const_iterator iter = state.collisions.begin(); iter != state.collisions.end(); ++iter)
{
if (!iter->expired())
{
actors->push_back(*iter);
}
}
}