Added files

This commit is contained in:
Simponic 2020-11-06 11:48:27 -07:00
commit f4e392912a
18 changed files with 533 additions and 0 deletions

3
makefile Normal file
View File

@ -0,0 +1,3 @@
CXX = g++
CXXFLAGS = -lsfml-graphics -lsfml-window -lsfml-system

BIN
sprites/bullet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 B

BIN
sprites/gunRight.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
sprites/player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

32
src/acceleration.cpp Normal file
View File

@ -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();
}

18
src/acceleration.h Normal file
View File

@ -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

7
src/bullet.h Normal file
View File

@ -0,0 +1,7 @@
#include "object.h"
class Bullet : public Object {
public:
Bullet(const Point &point, const Velocity &velocity) : Object(point, velocity) {
}
}

19
src/gun.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "point.h"
#include "object.h"
#include "gun.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
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);
}

14
src/gun.h Normal file
View File

@ -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

41
src/main.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <bits/stdc++.h>
#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;
}

120
src/object.cpp Normal file
View File

@ -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);
}

50
src/object.h Normal file
View File

@ -0,0 +1,50 @@
#ifndef OBJECT_H
#define OBJECT_H
#include "point.h"
#include "velocity.h"
#include "acceleration.h"
#include <SFML/Graphics.hpp>
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

88
src/player.cpp Normal file
View File

@ -0,0 +1,88 @@
#include "object.h"
#include "player.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <bits/stdc++.h>
#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()));
}

29
src/player.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef PLAYER_H
#define PLAYER_H
#include "object.h"
#include "point.h"
#include "velocity.h"
#include "gun.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
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

33
src/point.cpp Normal file
View File

@ -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();
}

20
src/point.h Normal file
View File

@ -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

38
src/velocity.cpp Normal file
View File

@ -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();
}

21
src/velocity.h Normal file
View File

@ -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