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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>
const float pi = 4 * std::atan(1);
class User : public sf::Drawable, public sf::Transformable
{
private :
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = NULL;
target.draw(m_Vertices, states);
}
sf::VertexArray m_Vertices;
float speed;
float rotation;
bool shoot = false;
sf::Time CoolDown;
public :
User() :
speed(0.5),
rotation(0.5)
{
m_Vertices.resize(3);
m_Vertices.setPrimitiveType(sf::Triangles);
//shapes
m_Vertices[0].position = sf::Vector2f(0, 0);
m_Vertices[1].position = sf::Vector2f(37, 18);
m_Vertices[2].position = sf::Vector2f(0, 37);
//color
m_Vertices[1].color = sf::Color(0, 0, 120, 255);
this->setOrigin(18, 18);
this->move(18, 18);
}
void Action(const sf::RenderWindow &window)
{
float radians = (this->getRotation() * pi) / 180;
float TempSpeed = speed;
sf::Vector2f OldPos = this->getPosition();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{
TempSpeed += 0.5;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sf::Vector2f direction;
direction.x = TempSpeed * std::cos(radians);
direction.y = TempSpeed * std::sin(radians);
this->move(direction);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
sf::Vector2f direction;
direction.x = -(TempSpeed * std::cos(radians));
direction.y = -(TempSpeed * std::sin(radians));
this->move(direction);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
this->rotate( -(rotation) );
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
this->rotate(rotation);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
if (this->CoolDown > sf::milliseconds(0)) return;
this->CoolDown = sf::milliseconds(200);
this->setShoot(true);
}
check(window, OldPos);
}
void check(const sf::RenderWindow& window, sf::Vector2f pos)
{
if (this->getPosition().x < 0) this->setPosition(pos);
else if (this->getPosition().x > window.getSize().x) this->setPosition(pos);
if (this->getPosition().y < 0) this->setPosition(pos);
else if (this->getPosition().y > window.getSize().y) this->setPosition(pos);
}
bool& getShoot()
{
return this->shoot;
}
void setShoot(bool val)
{
this->shoot = val;
}
void updateCooldown(sf::Time& elapsed)
{
this->CoolDown -= elapsed;
}
};
class Bullet
{
private :
sf::RectangleShape Rect;
float speed;
public :
Bullet(User& user) :
speed(5)
{
this->Rect.setSize(sf::Vector2f(10, 5));
this->Rect.setOrigin(Rect.getSize().x / 2, Rect.getSize().y / 2);
this->Rect.setPosition(user.getPosition());
this->Rect.setRotation(user.getRotation());
user.setShoot(false);
}
sf::Shape& getShape()
{
return Rect;
}
void update()
{
float radians = (Rect.getRotation() * pi) / 180;
sf::Vector2f direction;
direction.x = speed * std::cos(radians);
direction.y = speed * std::sin(radians);\
this->Rect.move(direction);
}
bool check(const sf::RenderWindow& window)
{
if (this->Rect.getPosition().x < 0) return true;
else if (this->Rect.getPosition().x > window.getSize().x) return true;
if (this->Rect.getPosition().y < 0) return true;
else if (this->Rect.getPosition().y > window.getSize().y) return true;
return false;
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(900, 600), "lol window");
User user;
std::vector <Bullet> bullet;
sf::Clock clock;
sf::Time elapsed;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) window.close();
}
if (bullet.size() == 0);
else
{
for (auto i = bullet.begin(); i != bullet.end(); ++i)
{
i->update();
if (i->check(window))
{
if (bullet.size() == 1) bullet.clear();
else bullet.erase(i);
}
}
}
user.Action(window);
if (user.getShoot())
{
bullet.push_back(Bullet(user));
}
elapsed = clock.restart();
user.updateCooldown(elapsed);
window.clear();
window.draw(user);
for (auto i : bullet)
{
window.draw(i.getShape());
}
window.display();
}
}
|