-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
193 lines (161 loc) · 6.19 KB
/
Player.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
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <glm\vec2.hpp>
#include <glm\gtc\quaternion.hpp>
#include "Config\PhysicsConfig.h"
#include "Data\TerrainTile.h"
#include "Generators\PhysicsGenerator.h"
#include "Math\PhysicsOps.h"
#include "Input.h"
#include "Map.h"
#include "Player.h"
Player::Player(ModelManager* modelManager, Physics* physics)
: gravityWeapon(physics), pressureWeapon(physics, glm::vec2(1.0f, 10.0f)), rockWeapon(modelManager, physics, glm::vec2(10.0f, 500.0f)), sunbeamWeapon(physics), // TODO configurable
lastMousePos(glm::ivec2(-1, -1)), camera(-80, glm::vec2(-30, 30), glm::vec2(-14, 14)), isOnGround(true), motionType(ON_FOOT), // TODO configurable camera.
enemyKos(0), allyKos(0), civilianKos(0), model()
{
selectedWeapon = &rockWeapon;
}
bool Player::LoadPlayerModel(ModelManager* modelManager)
{
model.modelId = modelManager->LoadModel("models/player");
return model.modelId != 0;
}
void Player::LoadPlayerPhysics(Physics* physics, glm::vec3 startingPosition, float mass)
{
model.body = PhysicsGenerator::GetDynamicBody(PhysicsGenerator::CShape::PLAYER, PhysicsOps::Convert(startingPosition), mass);
model.body->forceActivationState(DISABLE_DEACTIVATION);
// Rotate to face the forwards direction.
btTransform& worldTransform = model.body->getWorldTransform();
model.body->setAngularFactor(0.0f);
model.body->setFriction(2.0f); // TODO configurable.
model.body->setUserPointer(new TypedCallback<UserPhysics::ObjectType>(UserPhysics::ObjectType::PLAYER, this));
physics->AddBody(model.body);
camera.Initialize(model.body);
}
void Player::UnloadPlayerPhysics(Physics* physics)
{
// TODO cleanup the weapons.
physics->RemoveBody(model.body);
physics->DeleteBody(model.body, false);
}
void Player::Callback(UserPhysics::ObjectType collidingObject, void* callbackSpecificData)
{
if (collidingObject == UserPhysics::ObjectType::HEIGHTMAP)
{
isOnGround = true;
}
}
const glm::vec2 Player::GetTerrainPosition() const
{
glm::vec3 bodyPos = PhysicsGenerator::GetBodyPosition(model.body);
return glm::vec2(bodyPos.x, bodyPos.y);
}
const glm::vec3 Player::GetPosition() const
{
return PhysicsGenerator::GetBodyPosition(model.body);
}
const glm::quat Player::GetOrientation() const
{
return PhysicsGenerator::GetBodyRotation(model.body) * glm::rotate(glm::quat(), glm::radians(-90.0f), glm::vec3(1, 0, 0));
}
const glm::quat Player::GetViewOrientation() const
{
return camera.GetViewOrientation();
}
const glm::vec2 Player::Get2DOrientation() const
{
glm::vec3 forwardsVector = PhysicsOps::ForwardsVector(GetOrientation());
return glm::normalize(glm::vec2(forwardsVector.x, forwardsVector.y));
}
const glm::mat4 Player::GetViewMatrix() const
{
return camera.GetViewMatrix();
}
void Player::Render(ModelManager* modelManager, const glm::mat4& projectionMatrix)
{
modelManager->RenderModelImmediate(projectionMatrix, &model);
selectedWeapon->Render(projectionMatrix);
}
void Player::Update(float frameTime, int terrainTypeOn)
{
// Move the player around.
glm::quat orientation = GetOrientation();
glm::vec3 upVector = PhysicsOps::UpVector(orientation);
glm::vec3 forwardsVector = PhysicsOps::ForwardsVector(orientation);
glm::vec3 sidewaysVector = glm::cross(upVector, forwardsVector);
// TODO configurable.
forwardsVector = glm::normalize(forwardsVector);
if (Input::IsKeyPressed(GLFW_KEY_W))
{
model.body->applyCentralForce(PhysicsOps::Convert(forwardsVector * PhysicsConfig::ViewSidewaysSpeed));
}
if (Input::IsKeyPressed(GLFW_KEY_S))
{
model.body->applyCentralForce(PhysicsOps::Convert(-forwardsVector * PhysicsConfig::ViewSidewaysSpeed));
}
if (Input::IsKeyPressed(GLFW_KEY_A))
{
btTransform& worldTransform = model.body->getWorldTransform();
btQuaternion rotation = worldTransform.getRotation();
rotation = rotation * btQuaternion(btVector3(0, 0, 1), 0.05f); // TODO configurable
worldTransform.setRotation(rotation);
}
if (Input::IsKeyPressed(GLFW_KEY_D))
{
btTransform& worldTransform = model.body->getWorldTransform();
btQuaternion rotation = worldTransform.getRotation();
rotation = rotation * btQuaternion(btVector3(0, 0, 1), -0.05f); // TODO configurable
worldTransform.setRotation(rotation);
}
// TODO configurable (jump and force),
// Jump
if (isOnGround && Input::IsKeyPressed(GLFW_KEY_SPACE))
{
model.body->applyCentralImpulse(PhysicsOps::Convert(glm::vec3(0.0f, 0.0f, 200.0f)));
//isOnGround = false;
}
// Camera off-center offset.
if (Input::IsMouseButtonPressed(GLFW_MOUSE_BUTTON_RIGHT))
{
if (lastMousePos.x == -1)
{
lastMousePos = Input::GetMousePos();
}
else
{
glm::ivec2 newMousePos = Input::GetMousePos();
glm::ivec2 deltaPos = lastMousePos - newMousePos;
lastMousePos = newMousePos;
// Rotate the camera for x motion and y motion.
float xAmount = PhysicsConfig::ViewRotateAroundFactor * (float)deltaPos.x;
float yAmount = PhysicsConfig::ViewRotateUpFactor * (float)deltaPos.y;
// Rotate around
camera.Yaw(xAmount);
// Rotate up
camera.Pitch(yAmount);
}
}
else
{
lastMousePos = glm::ivec2(-1, -1);
}
// Limit player motion.
float speedLimitSqd = terrainTypeOn == TerrainTypes::ROADS ?
(Player::RoadSpeedLimit * Player::RoadSpeedLimit) : (Player::SpeedLimit * Player::SpeedLimit);
if (model.body->getLinearVelocity().length2() > speedLimitSqd)
{
btVector3 linearVelocity = model.body->getLinearVelocity();
model.body->setLinearVelocity(Player::SpeedLimit * linearVelocity.normalize());
}
camera.Update(frameTime);
selectedWeapon->Update(frameTime);
// Fire.
// TODO configurable.
if (Input::IsKeyTyped(GLFW_KEY_F))
{
// Fire a cube for collision tests.
glm::vec3 forwardsVector = PhysicsOps::ForwardsVector(orientation);
glm::vec3 pos = GetPosition() + 5.0f * forwardsVector;
selectedWeapon->Fire(pos, forwardsVector);
}
}