-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ship.cpp
127 lines (93 loc) · 2.61 KB
/
Ship.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
#include <iostream>
#include <vector>
#include <list>
#include "ship.hpp"
#include "Vector.hpp"
using std::cout;
using std::endl;
using std::vector;
using std::list;
Ship::Ship() : missile_id(-1), ship_id(0), fire_damage(0), fire_wait(0), fire_cost(0), hit_points(0), accelerating(0), acc_speed(0), max_speed(0), rotate(0), rotate_speed(0), max_distance(0), distance(0) {}
void Ship::Accelerating(double i) {
accelerating += i;
}
void Ship::PushCurrentShipCords() {
Last_ShipCords.push_front(ShipCords);
if (Last_ShipCords.size() > SHIP_TRACE)
Last_ShipCords.pop_back();
}
void Ship::Accelerate() {
for (vec_iter i = ship_vectors.begin(); i != ship_vectors.end(); i++) {
ship_vec.AddVector(*i);
}
ship_vec.LimitForce(max_speed);
}
void Ship::Rotate(double i) {
rotate += i;
}
void Ship::LoadSurface(char* filename, std::vector<SDL_Surface*>& surf) {
SDL_Surface* origin = CSurface::OnLoad(filename);
for (vsurf_sz i = 0; i <= 360; i++) {
SDL_Surface* tmp_surf = rotozoomSurface(origin, i, 1.0, 1);
surf.push_back(tmp_surf);
}
}
SDL_Surface* Ship::GetSurface(vsurf_sz degree) {
return ship_surf[degree];
}
void Ship::Rotate() {
ShipCords.degree += rotate;
if (ShipCords.degree < 0)
ShipCords.degree = 360 - rotate_speed;
else if (ShipCords.degree > 360)
ShipCords.degree = rotate_speed;
}
void Ship::NextShip() {
if (accelerating > 0) {
Vector acc_vec;
acc_vec.force = acc_speed;
acc_vec.degree = ShipCords.degree;
PushNewVector(acc_vec);
}
else if (accelerating < 0) {
Vector acc_vec;
acc_vec.force = acc_speed;
acc_vec.degree = ShipCords.degree - 180;
PushNewVector(acc_vec);
}
Rotate();
Accelerate();
PushCurrentShipCords();
assign_limited(ShipCords.x,ship_vec.X(),BOARD_X);
assign_limited(ShipCords.y,(-1 * ship_vec.Y()),BOARD_Y);
// adding to total distance
distance += ship_vec.force;
// lowering fire_wait so we can fire again
if (fire_wait > 0)
fire_wait--;
// cout << "distance: " << distance << endl;
}
void Ship::PushNewVector(Vector new_vec) {
ship_vectors.push_back(new_vec);
}
void Ship::ClearVectors() {
vector<Vector> new_ship_vectors;
ship_vectors = new_ship_vectors;
}
bool Ship::CanFire() {
return fire_wait == 0;
}
Ship Ship::Fire() {
Ship new_missile;
new_missile.ShipCords = ShipCords;
new_missile.ship_surf = missile_surf;
new_missile.max_speed = acc_speed * 10;
new_missile.max_distance = 500;
new_missile.missile_id = ship_id;
new_missile.hit_points = 1;
new_missile.ship_vec.force = acc_speed * 10;
new_missile.ship_vec.degree = ShipCords.degree;
new_missile.fire_damage = 10;
fire_wait = fire_cost;
return new_missile;
}