-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level.cpp
139 lines (125 loc) · 4.05 KB
/
Level.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
#include "Level.h"
Level::Level(HGE* _hge, int xRes, int yRes)
{
hge = _hge;
this->xRes = xRes;
this->yRes = yRes;
//set up level
gravity = new hgeVector(0,5);
terrain = new Terrain(xRes,yRes,_hge);
font = new hgeFont("font.fnt", true);
//set up gfx
particleManager = new hgeParticleManager();
HTEXTURE texture = hge->Texture_Load("ships.png");
hgeSprite* sprite = new hgeSprite(texture, 0,0,30,30);
sprite->SetHotSpot(15, 15);
collisionLarge = new hgeParticleSystem("particle9.psi", sprite);
//create a player & enemy
createPlayer(xRes/2, yRes/2);
createEnemy(100, 100);
}
Level::~Level(void)
{
}
/*Collides entity with terrain and rebounds it*/
bool Level::collideWithTerrain(Entity* entity)
{
float normal = terrain->findSurfaceCollisionNormal( entity->getXPos() + entity->bbox0->x,
entity->getYPos() + entity->bbox0->y,
entity->getXPos() + entity->bbox1->x,
entity->getYPos() + entity->bbox1->y,
hge);
if(normal == -1)
normal = terrain->findSurfaceCollisionNormal( entity->getXPos() + entity->bbox1->x,
entity->getYPos() + entity->bbox1->y,
entity->getXPos() + entity->bbox2->x,
entity->getYPos() + entity->bbox2->y,
hge);
if(normal == -1)
normal = terrain->findSurfaceCollisionNormal( entity->getXPos() + entity->bbox2->x,
entity->getYPos() + entity->bbox2->y,
entity->getXPos() + entity->bbox3->x,
entity->getYPos() + entity->bbox3->y,
hge);
if(normal == -1)
normal = terrain->findSurfaceCollisionNormal( entity->getXPos() + entity->bbox3->x,
entity->getYPos() + entity->bbox3->y,
entity->getXPos() + entity->bbox0->x,
entity->getYPos() + entity->bbox0->y,
hge);
if(normal != -1)
{
//hge->System_Log("Collision Normal : %f", normal);
//decode angle from bits
float collisionAngle = normal/255 * M_PI * 2;
//calculate the rebound velocity and apply it to the entity
hgeVector* normalVector = new hgeVector(-cos(collisionAngle), sin(collisionAngle));
hgeVector entityNormalizedVector = (player->getVelocity()) / player->getSpeed();
float dotProduct = normalVector->Dot(&entityNormalizedVector);
hgeVector reboundVector = player->getSpeed() * (-2 * dotProduct * (*normalVector) + entityNormalizedVector);
entity->setVelocity(reboundVector * 0.65);
terrain->makeHole(terrain->lastCollisionX, terrain->lastCollisionY, 4, hge);
particleManager->SpawnPS(&(collisionLarge->info), terrain->lastCollisionX, terrain->lastCollisionY);
return true;
}
return false;
}
bool Level::createPlayer(int xPos, int yPos)
{
HTEXTURE playerTexture = hge->Texture_Load("ships.png");
player = new Entity(xPos, yPos, 0.1f, playerTexture);
return true;
}
bool Level::createEnemy(int xPos, int yPos)
{
HTEXTURE enemyTexture = hge->Texture_Load("ships.png");
enemy = new Enemy( xPos + 100, yPos, 0.1f, enemyTexture);
return true;
}
bool Level::logicStep()
{
deltaT = hge->Timer_GetDelta();
processInput();
player->updatePosition(deltaT);
player->applyLinearForce(*gravity, deltaT);
collideWithTerrain(player);
enemy->updatePosition(deltaT);
enemy->applyLinearForce(*gravity, deltaT);
enemy->runAI(deltaT, terrain);
enemy->setTarget(player->getXPos(), player->getYPos());
collideWithTerrain(enemy);
return true;
}
bool Level::render()
{
player->drawSelf(deltaT, hge);
enemy->drawSelf(deltaT, hge);
terrain->render(hge);
particleManager->Update(deltaT);
particleManager->Render();
//draw FPS
char fps[10];
itoa((1/deltaT), fps, 10);
font->Render(0,0,HGETEXT_LEFT,fps);
//draw player speed
float playerX = player->getXPos();
float playerY = player->getYPos();
itoa(player->getSpeed(), fps, 10);
font->Render(0,50,HGETEXT_LEFT, fps);
return true;
}
void Level::processInput()
{
if (hge->Input_GetKeyState(HGEK_LEFT))
{
player->applyAngularForce(-1.4f, deltaT);
}
if (hge->Input_GetKeyState(HGEK_RIGHT))
{
player->applyAngularForce(1.4f, deltaT);
}
if (hge->Input_GetKeyState(HGEK_UP))
{
player->thrust(deltaT);
}
}