commit f4e392912a3812b602f787c6db30296cea14962e Author: Simponic Date: Fri Nov 6 11:48:27 2020 -0700 Added files diff --git a/makefile b/makefile new file mode 100644 index 0000000..3ea6686 --- /dev/null +++ b/makefile @@ -0,0 +1,3 @@ +CXX = g++ +CXXFLAGS = -lsfml-graphics -lsfml-window -lsfml-system + diff --git a/sprites/bullet.png b/sprites/bullet.png new file mode 100644 index 0000000..7f144b8 Binary files /dev/null and b/sprites/bullet.png differ diff --git a/sprites/gunRight.png b/sprites/gunRight.png new file mode 100755 index 0000000..ef54df8 Binary files /dev/null and b/sprites/gunRight.png differ diff --git a/sprites/player.png b/sprites/player.png new file mode 100644 index 0000000..eff556a Binary files /dev/null and b/sprites/player.png differ diff --git a/src/acceleration.cpp b/src/acceleration.cpp new file mode 100644 index 0000000..92dd2ac --- /dev/null +++ b/src/acceleration.cpp @@ -0,0 +1,32 @@ +#include "acceleration.h" + +Acceleration :: Acceleration() { + setD2x(0.0f); + setD2y(0.0f); +} + +Acceleration :: Acceleration(const float d2x, const float d2y) { + setD2x(d2x); + setD2y(d2y); +} + +void Acceleration :: setD2x(const float d2x) { + this->d2x = d2x; +} + +void Acceleration :: setD2y(const float d2y) { + this->d2y = d2y; +} + +float Acceleration :: getD2x() { + return this->d2x; +} + +float Acceleration :: getD2y() { + return this->d2y; +} + +void Acceleration :: addAcceleration(Acceleration &acc) { + this->d2x += acc.getD2x(); + this->d2y += acc.getD2y(); +} diff --git a/src/acceleration.h b/src/acceleration.h new file mode 100644 index 0000000..0186a19 --- /dev/null +++ b/src/acceleration.h @@ -0,0 +1,18 @@ +#ifndef ACCELERATION_H +#define ACCELERATION_H + +class Acceleration { + private: + float d2x; + float d2y; + public: + Acceleration(); + Acceleration(const float d2x, const float d2y); + void setD2x(const float d2x); + void setD2y(const float d2y); + float getD2x(); + float getD2y(); + void addAcceleration(Acceleration &acc); +}; + +#endif diff --git a/src/bullet.h b/src/bullet.h new file mode 100644 index 0000000..6aee4b0 --- /dev/null +++ b/src/bullet.h @@ -0,0 +1,7 @@ +#include "object.h" + +class Bullet : public Object { + public: + Bullet(const Point &point, const Velocity &velocity) : Object(point, velocity) { + } +} diff --git a/src/gun.cpp b/src/gun.cpp new file mode 100644 index 0000000..312e1b1 --- /dev/null +++ b/src/gun.cpp @@ -0,0 +1,19 @@ +#include "point.h" +#include "object.h" +#include "gun.h" +#include +#include + +Gun :: Gun() : Object() {} + +Gun :: Gun(const Point &point, const sf::Texture &texture, const int width, const int height) : Object(point) { + setTexture(texture); + this->sprite.setTexture(texture); + this->sprite.setOrigin(sf::Vector2f(width / 2 - 20, height / 2)); + setWidth(width); + setHeight(height); +} + +void Gun :: draw(sf::RenderWindow &window) { + window.draw(sprite); +} diff --git a/src/gun.h b/src/gun.h new file mode 100644 index 0000000..2642df0 --- /dev/null +++ b/src/gun.h @@ -0,0 +1,14 @@ +#ifndef GUN_H +#define GUN_H + +#include "point.h" +#include "object.h" +#include "SFML/Graphics.hpp" + +class Gun : public Object { + public: + Gun(); + Gun(const Point &point, const sf::Texture &texture, const int width, const int height); + void draw(sf::RenderWindow &window); +}; +#endif diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d780ea0 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include "player.h" +#include "gun.h" +#include "point.h" +#include "velocity.h" + +float width = 1280; +float height = 720; +std::string window_name = "Toxy"; + +int main() +{ + sf::RenderWindow window(sf::VideoMode(width, height), window_name); + window.setVerticalSyncEnabled(true); // V-Sync enabled + float angle; + int mouseX, mouseY; + sf::Color color(8, 105, 201); + sf::Texture gunTexture; + gunTexture.loadFromFile("sprites/gunRight.png"); + Player player(Point(width / 2, height / 2), Velocity(0,0), color, Gun(player.getPoint(), gunTexture, 150, 45)); + + while (window.isOpen()) + { + sf::Event event; + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + window.close(); + } + window.clear(sf::Color(184, 184, 184)); + mouseX = sf::Mouse::getPosition(window).x - width / 2; + mouseY = sf::Mouse::getPosition(window).y - height / 2; + player.setAngle(atan2(mouseY,mouseX)); + player.draw(window); + window.display(); + } + + return 0; +} diff --git a/src/object.cpp b/src/object.cpp new file mode 100644 index 0000000..5f1fac7 --- /dev/null +++ b/src/object.cpp @@ -0,0 +1,120 @@ +#include "object.h" +#include "point.h" +#include "velocity.h" +#include "acceleration.h" + +Object :: Object() { + setAngle(0.0f); + setWidth(0); + setHeight(0); + setPoint(Point()); + setVelocity(Velocity()); + setAcceleration(Acceleration()); + setAlive(true); +} + +Object :: Object(const Point &point) { + setAngle(0.0f); + setWidth(0); + setHeight(0); + setPoint(point); + setVelocity(Velocity()); + setAcceleration(Acceleration()); + setAlive(true); +} + +Object :: Object(const Point &point, const Velocity &velocity) { + setAngle(0.0f); + setWidth(0); + setHeight(0); + setPoint(point); + setVelocity(velocity); + setAcceleration(Acceleration()); + setAlive(true); +} +Object :: Object(const Point &point, const Velocity &velocity, const Acceleration &acceleration) { + setAngle(0.0f); + setWidth(0); + setHeight(0); + setPoint(point); + setVelocity(velocity); + setAcceleration(acceleration); + setAlive(true); +} + +void Object :: setAngle(const float angle) { + this->angle = angle; +} + +void Object :: setWidth(const int width) { + this->width = width; +} + +void Object :: setHeight(const int height) { + this->height = height; +} + +void Object :: setPoint(const Point &point) { + this->point = point; +} + +void Object :: setVelocity(const Velocity &velocity) { + this->velocity = velocity; +} + +void Object :: setAcceleration(const Acceleration &acceleration) { + this->acceleration = acceleration; +} + +void Object :: setAlive(const bool alive) { + this->alive = alive; +} + +void Object :: setTexture(const sf::Texture &texture) { + this->texture = texture; +} + +void Object :: setSprite(const sf::Sprite &sprite) { + this->sprite = sprite; +} + +float Object :: getAngle() { + return this->angle; +} + +int Object :: getWidth() { + return this->width; +} + +int Object :: getHeight() { + return this->height; +} + +Point Object :: getPoint() { + return this->point; +} + +Velocity Object :: getVelocity() { + return this->velocity; +} + +Acceleration Object :: getAcceleration() { + return this->acceleration; +} + +bool Object :: getAlive() { + return this->alive; +} + +sf::Texture Object :: getTexture() { + return this->texture; +} + +sf::Sprite Object :: getSprite() { + return this->sprite; +} + +void Object :: kill() { + setAlive(false); +} + diff --git a/src/object.h b/src/object.h new file mode 100644 index 0000000..b29c332 --- /dev/null +++ b/src/object.h @@ -0,0 +1,50 @@ +#ifndef OBJECT_H +#define OBJECT_H + +#include "point.h" +#include "velocity.h" +#include "acceleration.h" +#include + +class Object { + protected: + int width, height; + float angle; + Point point; + Velocity velocity; + Acceleration acceleration; + bool alive; + sf::Texture texture; + sf::Sprite sprite; + public: + Object(); + Object(const Point &point); + Object(const Point &point, const Velocity &velocity); + Object(const Point &point, const Velocity &velocity, const Acceleration &acc); + void setAngle(const float angle); + void setWidth(const int width); + void setHeight(const int height); + void setPoint(const Point &point); + void setVelocity(const Velocity &velocity); + void setAcceleration(const Acceleration &acc); + void setAlive(const bool alive); + void setTexture(const sf::Texture &texture); + void setSprite(const sf::Sprite &sprite); + float getAngle(); + int getWidth(); + int getHeight(); + Point getPoint(); + Velocity getVelocity(); + Acceleration getAcceleration(); + bool getAlive(); + sf::Texture getTexture(); + sf::Sprite getSprite(); + void kill(); + + virtual void update() { + this->velocity.update(this->acceleration); + this->point.update(this->velocity); + } + virtual void draw(sf::RenderWindow &window) = 0; +}; +#endif diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..e99ea43 --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,88 @@ +#include "object.h" +#include "player.h" +#include +#include +#include +#include "gun.h" +#define PI 3.14159265 +using namespace std; + +Player :: Player (const Point &point, const Velocity &velocity, sf::Color &color, const Gun &gun) : Object(point, velocity) { + setWidth(100); + setHeight(150); + setHealth(100); + setColor(color); + setGun(gun); + sf::Texture playerTexture; + playerTexture.loadFromFile("sprites/player.png"); + setTexture(playerTexture); + this->sprite.setTexture(this->texture); + this->sprite.setColor(color); + this->sprite.setTextureRect(sf::IntRect(0,0,100,150)); // 1st frame + this->sprite.setOrigin(sf::Vector2f(50,75)); + setSprite(this->sprite); +} + +void Player :: setGun(const Gun &gun) { + this->gun = gun; +} + +void Player :: setHealth(const float health) { + this->health = health; +} + +void Player :: setColor(const sf::Color &color) { + this->color = color; +} + +sf::Color Player :: getColor() { + return this->color; +} + +float Player :: getHealth() { + return this->health; +} + +Gun Player :: getGun() { + return this->gun; +} + +void Player :: draw(sf::RenderWindow &window) { + // Draw a player to the window + this->sprite.setPosition(sf::Vector2f(this->point.getX(),this->point.getY())); + float ang = -180.0f / PI * this->getAngle(); + int frame; + if (ang > -30 && ang <= 30) { + frame = 4; + } + else if (ang > 30 && ang <= 60) { + frame = 0; + } + else if (ang > 60 && ang <= 120) { + frame = 1; + } + else if (ang > 120 && ang <= 150) { + frame = 2; + } + else if (ang > 150 || ang <= -150) { + frame = 3; + } + else if (ang > -150 && ang <= -120) { + frame = 7; + } + else if (ang > -120 && ang <= -60) { + frame = 6; + } + else if (ang > -60 && ang <= -30) { + frame = 5; + } + window.draw(this->sprite); + sf::Sprite gunSprite = this->gun.getSprite(); + gunSprite.setRotation(-ang); + gunSprite.setPosition(sf::Vector2f(this->point.getX(),this->point.getY() + 40)); + this->gun.setSprite(gunSprite); + + window.draw(this->gun.getSprite()); + this->sprite.setTextureRect(sf::IntRect((frame % 3) * this->getWidth(), + (frame / 3) * this->getHeight(), this->getWidth(), this->getHeight())); +} diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..7f7f7ce --- /dev/null +++ b/src/player.h @@ -0,0 +1,29 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include "object.h" +#include "point.h" +#include "velocity.h" +#include "gun.h" +#include +#include + + +class Player : public Object { + private: + float health; + Gun gun; + sf::Color color; + public: + Player (const Point &point, const Velocity &velocity, sf::Color &color, const Gun &gun); + void setGun(const Gun &gun); + void setHealth(const float health); + void setColor(const sf::Color &color); + float getHealth(); + sf::Color getColor(); + Gun getGun(); + + void draw(sf::RenderWindow &window); +}; + +#endif diff --git a/src/point.cpp b/src/point.cpp new file mode 100644 index 0000000..4b89a4a --- /dev/null +++ b/src/point.cpp @@ -0,0 +1,33 @@ +#include "point.h" +#include "velocity.h" + +Point :: Point() { + setX(0.0f); + setY(0.0f); +} + +Point :: Point (const float x, const float y) { + setX(x); + setY(y); +} + +void Point :: setX(const float x) { + this->x = x; +} + +void Point :: setY(const float y) { + this->y = y; +} + +float Point :: getX() { + return this->x; +} + +float Point :: getY() { + return this->y; +} + +void Point :: update(Velocity &vel) { + this->x += vel.getDx(); + this->y += vel.getDy(); +} diff --git a/src/point.h b/src/point.h new file mode 100644 index 0000000..31fdf3e --- /dev/null +++ b/src/point.h @@ -0,0 +1,20 @@ +#ifndef POINT_H +#define POINT_H + +#include "velocity.h" + +class Point { + private: + float x; + float y; + public: + Point(); + Point(const float x, const float y); + void setX(const float x); + void setY(const float y); + float getX(); + float getY(); + void update(Velocity &vel); +}; + +#endif diff --git a/src/velocity.cpp b/src/velocity.cpp new file mode 100644 index 0000000..71bf7cd --- /dev/null +++ b/src/velocity.cpp @@ -0,0 +1,38 @@ +#include "acceleration.h" +#include "velocity.h" + +Velocity :: Velocity() { + setDx(0.0f); + setDy(0.0f); +} + +Velocity :: Velocity(const float dx, const float dy) { + setDx(0.0f); + setDy(0.0f); +} + +void Velocity :: setDx(const float dx) { + this->dx = dx; +} + +void Velocity :: setDy(const float dy) { + this->dy = dy; +} + +float Velocity :: getDx() { + return this->dx; +} + +float Velocity :: getDy() { + return this->dy; +} + +void Velocity :: addVelocity (Velocity &vel) { + this->dx += vel.getDx(); + this->dy += vel.getDy(); +} + +void Velocity :: update(Acceleration &acc) { + this->dx += acc.getD2x(); + this->dy += acc.getD2y(); +} diff --git a/src/velocity.h b/src/velocity.h new file mode 100644 index 0000000..3c48949 --- /dev/null +++ b/src/velocity.h @@ -0,0 +1,21 @@ +#ifndef VELOCITY_H +#define VELOCITY_H + +#include "acceleration.h" + +class Velocity { + private: + float dx; + float dy; + public: + Velocity(); + Velocity(const float dx, const float dy); + void setDx(const float dx); + void setDy(const float dy); + float getDx(); + float getDy(); + void addVelocity(Velocity &vel); + void update(Acceleration &acc); +}; + +#endif