-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
144 lines (125 loc) · 3.81 KB
/
snake.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
#include "headers/snake.h"
void Key_Callback(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods);
Snake::Snake()
{
for(int i = 0; i < 5; i++)
{
mQuads.push_back(new Quad(-i,0));
mLastXPos.push_back(-i);
mLastYPos.push_back(0);
}
}
Snake::~Snake()
{
for(int i = 0; i < mQuads.size(); i++)
{
delete mQuads[i];
}
}
void Snake::Render()
{
for(int i = 0; i<mQuads.size(); i++)
{
mQuads[i]->Render();
if(!mDead)
{
mQuads[i]->SetColor(glm::vec3(1.0f,0.6f,0.3f));
}
else mQuads[i]->SetColor(glm::vec3(0.9f,0.1f,0.1f));
}
}
void Snake::Translate(int _x, int _y)
{
mQuads[0]->Translate(_x,_y);
for(int i = 1; i<mQuads.size(); i++)
{
mQuads[i]->SetPosition(mLastXPos[i-1],mLastYPos[i-1]);
}
for(int i = 0; i<mQuads.size(); i++)
{
mLastXPos[i] = mQuads[i]->GetXPos();
mLastYPos[i] = mQuads[i]->GetYPos();
}
}
void Snake::Update(double _deltaTime)
{
//std::cout << "Snake size : " << mQuads.size() << std::endl;
mTimer += _deltaTime;
if(mTimer >= 0.2 && !mDead)
{
switch(mDir)
{
case MOVE_RIGHT:
Translate(1,0);
break;
case MOVE_LEFT:
Translate(-1,0);
break;
case MOVE_UP:
Translate(0,1);
break;
case MOVE_DOWN:
Translate(0,-1);
break;
}
mTimer = 0;
}
if(Input::IsKeyPressedOnce(GLFW_KEY_UP) && mDir != MOVE_DOWN)
{
std::cout << "Up key pressed !" << std::endl;
mDir = MOVE_UP;
}
if(Input::IsKeyPressedOnce(GLFW_KEY_DOWN) && mDir != MOVE_UP)
{
std::cout << "Down key pressed !" << std::endl;
mDir = MOVE_DOWN;
}
if(Input::IsKeyPressedOnce(GLFW_KEY_RIGHT) && mDir != MOVE_LEFT)
{
std::cout << "Right key pressed !" << std::endl;
mDir = MOVE_RIGHT;
}
if(Input::IsKeyPressedOnce(GLFW_KEY_LEFT) && mDir != MOVE_RIGHT)
{
std::cout << "Left key pressed !" << std::endl;
mDir = MOVE_LEFT;
}
for(int i = 0; i<mQuads.size(); i++)
{
/*bool collisionX = mCoin->GetXPos() - 0.5f >= mQuads[i]->GetXPos() - 0.5f &&
mQuads[i]->GetXPos() - 0.5f >= mCoin->GetXPos() + 0.5f;
bool collisionY = mCoin->GetYPos() - 0.5f >= mQuads[i]->GetYPos() + 0.5f &&
mQuads[i]->GetYPos() - 0.5f >= mCoin->GetYPos() + 0.5f;
*/ if(mCoin->IsDetectable())
{
bool collisionX = mQuads[i]->GetXPos() - 0.5f >= mCoin->GetXPos() - 0.5f &&
mQuads[i] ->GetXPos() + 0.5f <= mCoin->GetXPos() + 0.5f;
bool collisionY = mQuads[i]->GetYPos() + 0.5f <= mCoin->GetYPos() + 0.5f &&
mQuads[i]->GetYPos() - 0.5f >= mCoin->GetYPos() - 0.5f;
if(collisionX && collisionY)
{
//std::cout << "Collision Happening" << std::endl;
mCoin->Hide();
mQuads.push_back(new Quad(mQuads[mQuads.size()-1]->GetXPos(),mQuads[mQuads.size()-1]->GetYPos()));
mLastXPos.push_back(mQuads[mQuads.size()-1]->GetXPos());
mLastYPos.push_back(mQuads[mQuads.size()-1]->GetYPos());
}
}
}
for(int j = 1; j<mQuads.size(); j++)
{
bool collisionX = mQuads[0]->GetXPos() - 0.5f >= mQuads[j]->GetXPos() - 0.5f &&
mQuads[0] ->GetXPos() + 0.5f <= mQuads[j]->GetXPos() + 0.5f;
bool collisionY = mQuads[0]->GetYPos() + 0.5f <= mQuads[j]->GetYPos() + 0.5f &&
mQuads[0]->GetYPos() - 0.5f >= mQuads[j]->GetYPos() - 0.5f;
if(collisionX && collisionY)
{
std::cout << "Damage was done" << std::endl;
mDead = true;
}
}
}
void Snake::SetCoin(Coin* _coin)
{
mCoin = _coin;
}