-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chain.cpp
116 lines (101 loc) · 3.06 KB
/
Chain.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
#include "Chain.h"
#include "config.h"
#include "util.h"
#include <algorithm>
#include <vector>
extern std::vector<Player*> allPlayers;
extern std::vector<GameObject*> allRenderObjects;
extern GameObject* referenceChain;
Chain::Chain(int player, glm::vec3 origin, glm::vec3 velocity)
: GameObject(*referenceChain){
owner = player;
mModel.position = origin;
vel = velocity;
prev = NULL;
next = NULL;
hook = NULL;
pulling = false;
mModel.rotation.y = glm::atan(vel.x, vel.z) + CHAIN_BASE_ROTATION;
mModel.scaling = glm::vec3(CHAIN_SCALING, CHAIN_SCALING, CHAIN_SCALING);
allRenderObjects.push_back(this);
}
Chain::Chain(int player, glm::vec3 origin, glm::vec3 velocity, Chain* next, bool grapple)
: Chain(player, origin, velocity){
grappling = grapple;
this->next = next;
next->prev = this;
}
Chain::Chain(int player, glm::vec3 origin, glm::vec3 velocity, Hook* hook, bool grapple)
: Chain(player, origin, velocity){
grappling = grapple;
this->hook = hook;
hook->prev = this;
}
void Chain::kill(){
// invalidate pointers
if(next != NULL) {
next->prev = prev;
} else {
hook->prev = prev;
}
if(prev != NULL) {
prev->next = next;
}
allRenderObjects.erase(std::remove(allRenderObjects.begin(), allRenderObjects.end(), this), allRenderObjects.end());
delete this;
}
void Chain::pull(){
pulling = true;
if(next != NULL) {
next->pull();
} else {
hook->pull();
}
}
void Chain::update(){
glm::vec3 follow;
if(pulling) {
if(grappling) {
// TODO : what happens here
} else {
// pull
Chain* p = prev;
do {
if (p != NULL) {
follow = p->mModel.position;
p = p->prev;
} else {
follow = allPlayers[owner]->hookpoint;
}
} while(glm::length(follow - mModel.position) == 0);
if (p == NULL) {
mModel.position += glm::normalize(follow - mModel.position) * CHAIN_PULL;
} else {
mModel.position = moveTowards(mModel.position, follow, CHAIN_BASE_PULL);
}
}
// update chain lasst because we are pulling
this->updateChainLinks();
} else {
// update chain first because we are pushing
this->updateChainLinks();
// push
if(next != NULL) {
follow = next->mModel.position;
} else {
follow = hook->mModel.position;
}
mModel.position = moveTowards(mModel.position, follow, CHAIN_BASE_PUSH);
}
glm::vec3 ne = (next != 0) ? next->mModel.position : hook->mModel.position;
glm::vec3 pr = (prev != 0) ? prev->mModel.position : allPlayers[owner]->hookpoint;
glm::vec3 dif = ne-pr;
mModel.rotation.y = glm::atan(dif.x, dif.z) + CHAIN_BASE_ROTATION + glm::pi<float>();
}
void Chain::updateChainLinks(){
if(next != NULL) {
next->update();
} else {
hook->update();
}
}