-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.cpp
151 lines (115 loc) · 4.5 KB
/
player.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
140
141
142
143
144
145
146
147
148
149
150
151
//
// Created by Sondre on 23.10.2017.
//
#include <iostream>
#include "player.h"
#include "gameConfig.h"
sf::Texture* player_texture_ptr;
// load something into the sound buffer...
player::player(int x, int y, int health) {
player_texture_ptr = &player_texture;
player_texture.loadFromFile("Assets/player.png");
this->rect.setTexture(player_texture_ptr);
this->rect.setPosition(x,y);
this->health = health;
this->maxHealth = health;
this->CollitionDamage = 10000;
this->rect.setSize(sf::Vector2f(100,100));
//this->rect.setSize(sf::Vector2f(player_texture_ptr->getSize().x, player_texture_ptr->getSize().y));
buffer.loadFromFile("Sounds/fighter_laser.wav");
sound.setBuffer(buffer);
sound.setVolume(50);
}
player::player() {
this->rect.setPosition(0,0);
this->health = this->maxHealth;
}
void player::ProcessEvents(sf::RenderWindow* window,float deltatime){
sf::Event event;
float movement_x = 0;
float movement_y = 0;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
movement_y = -deltatime*playerSpeed;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
movement_y = deltatime*playerSpeed;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
movement_x = -deltatime*playerSpeed;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
movement_x = deltatime*playerSpeed;
}
this->velocity.y = movement_y;
this->velocity.x = movement_x;
while (window->pollEvent(event)) {
switch (event.type) {
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::J:
if(Player_can_fire)
{
sound.play();
shoot(PlayerEquipment[0], sf::Vector2f(0.0f, -15.0f));
}
break;
case sf::Keyboard::K:
if(Player_can_fire)
{
sound.play();
shoot(PlayerEquipment[1], sf::Vector2f(0.0f, -15.0f));
}
break;
case sf::Keyboard::L:
if(Player_can_fire)
{
sound.play();
shoot(PlayerEquipment[2], sf::Vector2f(0.0f, -15.0f));
}
break;
}
break;
case sf::Event::Closed:
window->close();
break;
}
}
}
void player::moveOverride() {
PlayerEquipment[0]->rect.setPosition(this->rect.getPosition().x, this->rect.getPosition().y + 50);
PlayerEquipment[2]->rect.setPosition(this->rect.getPosition().x + 70, this->rect.getPosition().y + 50);
PlayerEquipment[1]->rect.setPosition(this->rect.getPosition().x + 35, this->rect.getPosition().y + 25);
}
void player::shoot(equipment* weapon, sf::Vector2f velocity)
{
if(weapon->clock.getElapsedTime().asSeconds() > weapon->FireRate) {
if(weapon->Name == "The Oblivirator")
{
if(this->Energy > 50){
Object *p = new Object(0, 0, 10000.0f, 10000.0f, weapon->projectile->ObjectTexture(), weapon->projectile->rect.getSize().x, weapon->projectile->rect.getSize().y);
p->CollitionDamage = weapon->BaseDamage + (weapon->Level * weapon->UpgradeScale);
p->rect.setPosition(0, weapon->getShape().getPosition().y);
p->velocity = velocity;
p->parent = this;
projectiles.push_back(p);
weapon->clock.restart();
this->Energy = this->Energy-50;
}
}
else
{
Object *p = new Object(0, 0, 1.0f, 1.0f, weapon->projectile->ObjectTexture(), weapon->projectile->rect.getSize().x, weapon->projectile->rect.getSize().y);
std::cout << weapon->BaseDamage << "+(" << weapon->Level << "*" << weapon->UpgradeScale << ")=" << weapon->BaseDamage + (weapon->Level * weapon->UpgradeScale) << std::endl;
p->CollitionDamage = weapon->BaseDamage + (weapon->Level * weapon->UpgradeScale);
p->rect.setPosition(weapon->getShape().getPosition().x + 10, weapon->getShape().getPosition().y);
p->velocity = velocity;
p->parent = this;
projectiles.push_back(p);
weapon->clock.restart();
}
}
}