-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectile.cpp
More file actions
65 lines (52 loc) · 1.38 KB
/
projectile.cpp
File metadata and controls
65 lines (52 loc) · 1.38 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
/*
Copyright (c) 2013 Auston Sterling
See license.txt for copying permission.
-----Projectile Class Definition-----
Auston Sterling
austonst@gmail.com
Contains the definition of the Projectile class.
*/
#ifndef _projectile_cpp_
#define _projectile_cpp_
#include <cmath>
#include "projectile.h"
//Default constructor; avoid
Projectile::Projectile() {};
//Regular constructor
Projectile::Projectile(Vec2f start, Fleet* dest, std::string effect, float speed):
pos_(start),
target_(dest),
speed_(DEFAULT_PROJECTILE_SPEED*speed),
effect_(effect),
lastTicks_(0) {}
//Updates the position of the projectile
void Projectile::update()
{
//First frame
if (lastTicks_ == 0)
{
lastTicks_ = SDL_GetTicks();
return;
}
//Find change in time
int newtime = SDL_GetTicks();
int dt = newtime - lastTicks_;
lastTicks_ = newtime;
//Move projectile towards destination
//Find target coordinates
Vec2f tar = target_->pos();
//Find the vector to apply
Vec2f diff = tar-pos_;
diff.normalize();
diff *= speed_*(dt/1000.0);
//Move it
pos_ += diff;
}
//Displays the projectile
void Projectile::display(SDL_Surface* screen, const SDL_Rect& camera)
{
//For now, just draw a rectangle
SDL_Rect rect = {Sint16(pos_.x() - 5 - camera.x), Sint16(pos_.y() - 5 - camera.y), 10, 10};
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
}
#endif