-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship.c
More file actions
117 lines (102 loc) · 2.9 KB
/
ship.c
File metadata and controls
117 lines (102 loc) · 2.9 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "utils.h"
#include "ship.h"
float traveledDistance = 0;
Ship* createShip(Position position) {
Ship* newship = malloc(sizeof (Ship));
newship->life = 100;
newship->position.x = position.x;
newship->position.y = position.y;
newship->position.z = position.z;
newship->velocity.x = 0;
newship->velocity.y = 0;
newship->velocity.z = INITIAL_VELOCITY;
return newship;
}
void killShip(Ship* sh) {
free(sh);
}
void updateVelocity(Ship *sh, Key key) {
switch (key) {
case UP:
if (sh->velocity.y + MOVING_FACTOR > MAX_XY_ORIENTATION)
return;
sh->velocity.y += MOVING_FACTOR;
break;
case DOWN:
if (sh->velocity.y - MOVING_FACTOR < -MAX_XY_ORIENTATION)
return;
sh->velocity.y -= MOVING_FACTOR;
break;
case RIGHT:
if (sh->velocity.x + MOVING_FACTOR > MAX_XY_ORIENTATION)
return;
sh->velocity.x += MOVING_FACTOR;
break;
case LEFT:
if (sh->velocity.x - MOVING_FACTOR < -MAX_XY_ORIENTATION)
return;
sh->velocity.x -= MOVING_FACTOR;
break;
case SPACE:
if (sh->velocity.z + VELOCITY_FACTOR > MAX_VELOCITY)
return;
sh->velocity.z += VELOCITY_FACTOR;
break;
default:
sh->velocity.x = 0;
sh->velocity.y = 0;
sh->velocity.z = INITIAL_VELOCITY;
break;
}
}
void updateShipPosition(Ship* sh) {
sh->position = spaceTimeEquation(sh->position, sh->velocity);
}
void updateScore(Ship* sh) {
traveledDistance = sh->position.z;
}
void insideKeeper(Ship *sh, Dimension dimension) {
if (sh->position.x < 0) {
sh->position.x = 0;
sh->velocity.x = 0;
}
else if (sh->position.x > dimension.x) {
sh->position.x = dimension.x;
sh->velocity.x = 0;
}
if (sh->position.y < 0) {
sh->position.y = 0;
sh->velocity.y = 0;
}
else if (sh->position.y > dimension.y) {
sh->position.y = dimension.y;
sh->velocity.y = 0;
}
}
Shot* shootFromShip(Position aimP, Position shipP, int power) {
Shot* newShot;
Position shotP = shipP;
Velocity shotV;
shotV.x = aimP.x - shipP.x;
shotV.y = aimP.y - shipP.y;
shotV.z = sqrt(SHIP_SHOT_VELOCITY * SHIP_SHOT_VELOCITY - shotV.x * shotV.x - shotV.y * shotV.y);
newShot = createShot(shotP, shotV, power);
return newShot;
}
void gotDamagedShip(Ship* sh, int damage) {
sh->life -= damage;
}
BOOL isShipAlive(Ship* sh) {
return sh->life > 0;
}
void renderShip(Ship* sh) {
glPushMatrix();
glTranslatef(sh->position.x, sh->position.y, 0);
//glRotated(2, 0, 1, 0);
glColor3f(0.2, 0.56, 0.);
glutSolidCube(0.2);
glPopMatrix();
}