-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweapon.cpp
More file actions
78 lines (66 loc) · 2.09 KB
/
weapon.cpp
File metadata and controls
78 lines (66 loc) · 2.09 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
/*
* Copyright (c) 2014 Ross Simpson
* All Right Reserved
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
* To view a copy of this license,visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*/
#include "weapon.h"
#include "gamestate.h"
#include <SDL2/SDL_image.h>
#include <string>
using namespace std;
bool StandardLaserWeapon::init()
{
// TODO: TEXTURE HANDLER!
// The strong argument for implementing a texture handler NOW, is because if this
// weapon is swapped out, the destructor will be called, freeing the bullet texture,
// meaning that all bullets currently on screen using the texture will not render
const string bulletTextureFile = "/home/rsimpson/Development/EndlessQuest/images/laser.png";
bulletTexture = IMG_LoadTexture(GameState::get()->getRenderer(), bulletTextureFile.c_str());
if (bulletTexture == nullptr)
{
logSDLError(cout, "Background::init(), IMG_LoadTexture");
return false;
}
return true;
}
void StandardLaserWeapon::update()
{
Vector2D velocity(0, -5);
// TODO: Clean up bullet when it exits screen
for (vector<Vector2D*>::iterator bullet = bullets.begin(); bullet != bullets.end(); ++bullet)
{
Vector2D *tmpBullet = (*bullet);
*tmpBullet += velocity;
}
}
void StandardLaserWeapon::fire(Vector2D shipPosition)
{
// TODO: Implement a rate of fire for weapons
Vector2D *bullet1 = new Vector2D(0, 0);
bullet1->setX(shipPosition.getX());
bullet1->setY(shipPosition.getY());
bullets.push_back(bullet1);
Vector2D *bullet2 = new Vector2D(0, 0);
bullet2->setX(shipPosition.getX()+50);
bullet2->setY(shipPosition.getY());
bullets.push_back(bullet2);
}
StandardLaserWeapon::~StandardLaserWeapon()
{
SDL_DestroyTexture(bulletTexture);
}
void StandardLaserWeapon::render(SDL_Renderer *renderer)
{
for (vector<Vector2D*>::iterator bullet = bullets.begin(); bullet != bullets.end(); ++bullet)
{
Vector2D *tmpBullet = (*bullet);
SDL_Rect dest;
dest.x = tmpBullet->getX();
dest.y = tmpBullet->getY();
dest.w = 48;
dest.h = 82;
SDL_RenderCopy(renderer, bulletTexture, NULL, &dest);
}
}