Statek + Strzelanie (do naprawienia białe kwadraty)

This commit is contained in:
2024-11-15 14:22:52 +01:00
parent 8ecd82eb4e
commit ce2924e635
12 changed files with 203 additions and 25 deletions

View File

@@ -1 +1,44 @@
#include "../headers/actor.h"
#include "../headers/Actor.h"
Actor::Actor(int x, int y, std::string path) {
loadTexture(path);
position.x = x;
position.y = y;
actorSprite.setPosition(x, y);
}
void Actor::loadTexture(std::string path) {
actorTexture.loadFromFile(path);
actorSprite.setTexture(actorTexture);
}
sf::Sprite &Actor::getSprite() {
return actorSprite;
}
void Actor::move(float deltaX, float deltaY) {
actorSprite.move(deltaX, deltaY);
}
void Actor::moveLeft() {
move(-10.0f, 0.0f);
position.x -= 10;
}
void Actor::moveRight() {
move(10.0f, 0.0f);
position.x += 10;
}
Position Actor::getPosition() {
return {position.x, position.y};
}
void Actor::shoot() {
bullets.emplace_back(position.x + actorSprite.getGlobalBounds().width / 2-62, position.y);
}
std::vector<Bullet> &Actor::getBullets() {
return bullets;
}

29
sources/Bullet.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <iostream>
#include "../headers/Bullet.h"
Bullet::Bullet(float x, float y) {
outOfBounds = false;
bulletTexture.loadFromFile("../assets/img/bullet.png");
bulletSprite.setTexture(bulletTexture);
bulletSprite.setPosition(x, y);
bulletSpeed = -10.0f;
bulletPosition.x = x;
bulletPosition.y = y;
}
void Bullet::setSpeed(float speed) {
bulletSpeed = speed;
}
sf::Sprite &Bullet::getSprite() {
return bulletSprite;
}
void Bullet::update() {
bulletSprite.move(0.0f, bulletSpeed);
bulletPosition.y += bulletSpeed;
if(bulletPosition.y < -100) {
outOfBounds = true;
std::cout << "Bullet out of bounds\n";
}
}

3
sources/Player.cpp Normal file
View File

@@ -0,0 +1,3 @@
#include "../headers/Player.h"
Player::Player(int x, int y, std::string path) : Actor(x, y, path) {};