Regroup games into one repo
1
ping_pong/.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
assets/fonts/Rajdhani/*.ttf filter=lfs diff=lfs merge=lfs -text
|
||||||
32
ping_pong/.gitignore
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
*.smod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
14
ping_pong/Makefile
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
CXX=g++
|
||||||
|
CXXFLAGS=-Wall -g -std=c++11
|
||||||
|
SFML_LIBS=-lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
|
||||||
|
|
||||||
|
all: ping_pong
|
||||||
|
|
||||||
|
ping_pong: src/main.o src/core.o
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $^ $(SFML_LIBS)
|
||||||
|
|
||||||
|
%.o: %.cpp %.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f ping_pong *.o
|
||||||
BIN
ping_pong/assets/fonts/Rajdhani/Rajdhani-Bold.ttf
(Stored with Git LFS)
Normal file
BIN
ping_pong/assets/fonts/Rajdhani/Rajdhani-Light.ttf
(Stored with Git LFS)
Normal file
BIN
ping_pong/assets/fonts/Rajdhani/Rajdhani-Medium.ttf
(Stored with Git LFS)
Normal file
BIN
ping_pong/assets/fonts/Rajdhani/Rajdhani-Regular.ttf
(Stored with Git LFS)
Normal file
BIN
ping_pong/assets/fonts/Rajdhani/Rajdhani-SemiBold.ttf
(Stored with Git LFS)
Normal file
41
ping_pong/include/core.hpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
|
|
||||||
|
struct Vector2 {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Entity {
|
||||||
|
Vector2 position;
|
||||||
|
sf::RectangleShape shape;
|
||||||
|
float speed;
|
||||||
|
Vector2 direction;
|
||||||
|
Vector2 velocity;
|
||||||
|
|
||||||
|
virtual void update(sf::Time delta) = 0;
|
||||||
|
void set_position(Vector2 new_pos);
|
||||||
|
void set_size(Vector2 new_size);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Bat : public Entity {
|
||||||
|
bool moving_right = false;
|
||||||
|
bool moving_left = false;
|
||||||
|
|
||||||
|
void update(sf::Time delta) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
enum Rebounds {
|
||||||
|
Sides,
|
||||||
|
Bat_Or_Top,
|
||||||
|
Bottom
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Ball : public Entity {
|
||||||
|
void update(sf::Time delta) override;
|
||||||
|
|
||||||
|
void rebound(Rebounds rb);
|
||||||
|
};
|
||||||
|
|
||||||
50
ping_pong/src/core.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "../include/core.hpp"
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
void Entity::set_position(Vector2 new_pos) {
|
||||||
|
position = new_pos;
|
||||||
|
shape.setPosition(position.x, position.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::set_size(Vector2 new_size) {
|
||||||
|
shape.setSize(sf::Vector2f{new_size.x, new_size.y});
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bat::update(sf::Time delta) {
|
||||||
|
if (moving_left) {
|
||||||
|
position.x -= speed * delta.asSeconds();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (moving_right) {
|
||||||
|
position.x += speed * delta.asSeconds();
|
||||||
|
}
|
||||||
|
|
||||||
|
set_position(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Ball::update(sf::Time delta) {
|
||||||
|
position.y += direction.y * speed * delta.asSeconds();
|
||||||
|
position.x += direction.x * speed * delta.asSeconds();
|
||||||
|
set_position(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ball::rebound(Rebounds rb) {
|
||||||
|
switch (rb) {
|
||||||
|
case Rebounds::Sides:
|
||||||
|
direction.x = -direction.x;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Rebounds::Bat_Or_Top:
|
||||||
|
direction.y = -direction.y;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Rebounds::Bottom:
|
||||||
|
position.y = 0;
|
||||||
|
position.x = 500;
|
||||||
|
direction.y = -direction.y;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
133
ping_pong/src/main.cpp
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
#include "../include/core.hpp"
|
||||||
|
#include <SFML/Window/WindowStyle.hpp>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <sstream>
|
||||||
|
#include <SFML/Audio/SoundBuffer.hpp>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <SFML/Audio.hpp>
|
||||||
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <SFML/Window/VideoMode.hpp>
|
||||||
|
|
||||||
|
Vector2 aspect_ratio = Vector2{1920, 1080};
|
||||||
|
|
||||||
|
const int MAX_ENTITIES = 2;
|
||||||
|
Entity *entities[MAX_ENTITIES];
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
sf::VideoMode vm(aspect_ratio.x, aspect_ratio.y);
|
||||||
|
sf::RenderWindow window(vm, "Pong", sf::Style::Default);
|
||||||
|
int score = 0;
|
||||||
|
int lives = 3;
|
||||||
|
|
||||||
|
// Bottom center of the screen
|
||||||
|
Bat bat;
|
||||||
|
bat.set_size(Vector2{50, 5});
|
||||||
|
bat.set_position(Vector2{aspect_ratio.x / 2, aspect_ratio.y - 20});
|
||||||
|
bat.speed = 300.0f;
|
||||||
|
|
||||||
|
Ball ball;
|
||||||
|
ball.set_size(Vector2{10, 10});
|
||||||
|
ball.set_position(Vector2{aspect_ratio.x / 2, 0});
|
||||||
|
ball.direction = Vector2{0.2f, 0.2f};
|
||||||
|
ball.speed = 1000.0f;
|
||||||
|
|
||||||
|
entities[0] = &bat;
|
||||||
|
entities[1] = &ball;
|
||||||
|
|
||||||
|
sf::Text hud;
|
||||||
|
sf::Font font;
|
||||||
|
font.loadFromFile("assets/fonts/Rajdhani/Rajdhani-Regular.ttf");
|
||||||
|
hud.setFont(font);
|
||||||
|
hud.setCharacterSize(75);
|
||||||
|
hud.setFillColor(sf::Color::White);
|
||||||
|
hud.setPosition(20, 20);
|
||||||
|
|
||||||
|
sf::Clock clock;
|
||||||
|
|
||||||
|
|
||||||
|
while (window.isOpen()) {
|
||||||
|
// INPUT
|
||||||
|
{
|
||||||
|
sf::Event event;
|
||||||
|
while (window.pollEvent(event)) {
|
||||||
|
if (event.type == sf::Event::Closed) {
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||||
|
bat.moving_left = true;
|
||||||
|
} else {
|
||||||
|
bat.moving_left = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
||||||
|
bat.moving_right = true;
|
||||||
|
} else {
|
||||||
|
bat.moving_right = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// UPDATE
|
||||||
|
{
|
||||||
|
sf::Time delta = clock.restart();
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_ENTITIES; i++) {
|
||||||
|
entities[i]->update(delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Score: " << score << " Lives: " << lives;
|
||||||
|
hud.setString(ss.str());
|
||||||
|
|
||||||
|
{
|
||||||
|
sf::FloatRect bounds = ball.shape.getGlobalBounds();
|
||||||
|
|
||||||
|
if (bounds.top > window.getSize().y) {
|
||||||
|
ball.rebound(Rebounds::Bottom);
|
||||||
|
lives--;
|
||||||
|
|
||||||
|
if (lives < 1) {
|
||||||
|
score = 0;
|
||||||
|
lives = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bounds.top < 0) {
|
||||||
|
ball.rebound(Rebounds::Bat_Or_Top);
|
||||||
|
score++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bounds.left < 0 || bounds.left + bounds.width > window.getSize().x) {
|
||||||
|
ball.rebound(Rebounds::Sides);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bounds.intersects(bat.shape.getGlobalBounds())) {
|
||||||
|
ball.rebound(Rebounds::Bat_Or_Top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// DRAW
|
||||||
|
{
|
||||||
|
window.clear();
|
||||||
|
window.draw(hud);
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_ENTITIES; i++) {
|
||||||
|
window.draw(entities[i]->shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
32
timber/.gitignore
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
*.smod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
14
timber/Makefile
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
CXX=g++
|
||||||
|
CXXFLAGS=-Wall -g -std=c++11
|
||||||
|
SFML_LIBS=-lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
|
||||||
|
|
||||||
|
all: timber
|
||||||
|
|
||||||
|
timber: src/main.o src/core.o
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $^ $(SFML_LIBS)
|
||||||
|
|
||||||
|
%.o: %.cpp %.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f timber *.o
|
||||||
BIN
timber/assets/fonts/KOMIKAP_.ttf
Normal file
BIN
timber/assets/graphics/axe.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
timber/assets/graphics/background.png
Normal file
|
After Width: | Height: | Size: 138 KiB |
BIN
timber/assets/graphics/bee.png
Normal file
|
After Width: | Height: | Size: 917 B |
BIN
timber/assets/graphics/branch.png
Normal file
|
After Width: | Height: | Size: 5.3 KiB |
BIN
timber/assets/graphics/cloud.png
Normal file
|
After Width: | Height: | Size: 5.3 KiB |
BIN
timber/assets/graphics/log.png
Normal file
|
After Width: | Height: | Size: 6.1 KiB |
BIN
timber/assets/graphics/player.png
Normal file
|
After Width: | Height: | Size: 3.1 KiB |
BIN
timber/assets/graphics/rip.png
Normal file
|
After Width: | Height: | Size: 5.1 KiB |
BIN
timber/assets/graphics/tree.png
Normal file
|
After Width: | Height: | Size: 38 KiB |
BIN
timber/assets/graphics/tree2.png
Normal file
|
After Width: | Height: | Size: 40 KiB |
BIN
timber/assets/sound/chop.wav
Normal file
BIN
timber/assets/sound/death.wav
Normal file
BIN
timber/assets/sound/out_of_time.wav
Normal file
87
timber/include/core.hpp
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
#ifndef CORE_H
|
||||||
|
#define CORE_H
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Color.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
enum class Side { left, right, none };
|
||||||
|
|
||||||
|
struct Vector2 {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AspectRatio {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TextData {
|
||||||
|
std::string content;
|
||||||
|
int size;
|
||||||
|
sf::Color color;
|
||||||
|
sf::Font *font;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct Text {
|
||||||
|
sf::Font font;
|
||||||
|
sf::Text text;
|
||||||
|
Vector2 position;
|
||||||
|
|
||||||
|
void set_color(sf::Color color);
|
||||||
|
|
||||||
|
void set_font(std::string path);
|
||||||
|
|
||||||
|
void set_string(std::string str);
|
||||||
|
|
||||||
|
void set_size(int size);
|
||||||
|
|
||||||
|
void set_position(Vector2 pos);
|
||||||
|
|
||||||
|
void set_origin(Vector2 origin);
|
||||||
|
|
||||||
|
sf::FloatRect get_rect();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Entity {
|
||||||
|
sf::Texture texture;
|
||||||
|
sf::Sprite sprite;
|
||||||
|
Vector2 position;
|
||||||
|
|
||||||
|
void set_texture(std::string texture);
|
||||||
|
|
||||||
|
void set_position(Vector2 pos);
|
||||||
|
|
||||||
|
void set_origin(Vector2 origin);
|
||||||
|
|
||||||
|
Vector2 get_position();
|
||||||
|
|
||||||
|
void set_rotation(int angle);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Player : public Entity {
|
||||||
|
Side side = Side::left;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Actor : public Entity {
|
||||||
|
bool active = false;
|
||||||
|
float speed = 0.0f;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Log : public Actor {
|
||||||
|
float speed_x = 1000;
|
||||||
|
float speed_y = -1500;
|
||||||
|
};
|
||||||
|
|
||||||
|
void update_branches(int seed);
|
||||||
|
|
||||||
|
void draw(sf::RenderWindow *window, Entity *entity);
|
||||||
|
|
||||||
|
void draw(sf::RenderWindow *window, Text *text);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // CORE_H
|
||||||
63
timber/src/core.cpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#include "../include/core.hpp"
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
void Entity::set_texture(std::string filename) {
|
||||||
|
texture.loadFromFile(filename);
|
||||||
|
sprite.setTexture(texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::set_rotation(int angle) {
|
||||||
|
sprite.setRotation(angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Entity::get_position() {
|
||||||
|
sf::Vector2f pos = sprite.getPosition();
|
||||||
|
return Vector2{pos.x, pos.y};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::set_position(Vector2 origin) {
|
||||||
|
sprite.setPosition(origin.x, origin.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::set_origin(Vector2 origin) {
|
||||||
|
sprite.setOrigin(origin.x, origin.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(sf::RenderWindow *window, Entity *entity) {
|
||||||
|
window->draw(entity->sprite);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(sf::RenderWindow *window, Text *text) {
|
||||||
|
window->draw(text->text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::set_font(std::string filename) {
|
||||||
|
font.loadFromFile(filename);
|
||||||
|
text.setFont(font);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::set_string(std::string str) {
|
||||||
|
text.setString(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::set_size(int size) {
|
||||||
|
text.setCharacterSize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::set_color(sf::Color color) {
|
||||||
|
text.setFillColor(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::set_origin(Vector2 origin) {
|
||||||
|
text.setOrigin(origin.x, origin.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::set_position(Vector2 origin) {
|
||||||
|
text.setPosition(origin.x, origin.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::FloatRect Text::get_rect() {
|
||||||
|
return text.getLocalBounds();
|
||||||
|
}
|
||||||
401
timber/src/main.cpp
Normal file
@ -0,0 +1,401 @@
|
|||||||
|
#include "../include/core.hpp"
|
||||||
|
#include <SFML/Audio/SoundBuffer.hpp>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <SFML/Audio.hpp>
|
||||||
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
int player_score = 0;
|
||||||
|
char player_initial = 'N';
|
||||||
|
float value_pi = 3.141f;
|
||||||
|
bool is_alive = true;
|
||||||
|
|
||||||
|
const int NUM_BRANCHES = 6;
|
||||||
|
Entity branches[NUM_BRANCHES];
|
||||||
|
Side branch_positions[NUM_BRANCHES];
|
||||||
|
|
||||||
|
const int NUM_CLOUDS = 3;
|
||||||
|
Actor clouds[NUM_CLOUDS];
|
||||||
|
Vector2 cloud_positions[NUM_CLOUDS] {
|
||||||
|
Vector2{0, 0},
|
||||||
|
Vector2{0, 250},
|
||||||
|
Vector2{0, 500}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
AspectRatio screen_size{1920, 1080};
|
||||||
|
sf::VideoMode vm(screen_size.x, screen_size.y);
|
||||||
|
sf::RenderWindow window(vm, "Timber", sf::Style::Fullscreen);
|
||||||
|
|
||||||
|
Entity bg;
|
||||||
|
bg.set_texture("assets/graphics/background.png");
|
||||||
|
bg.set_position(Vector2{0, 0});
|
||||||
|
|
||||||
|
Actor bee;
|
||||||
|
bee.set_texture("assets/graphics/bee.png");
|
||||||
|
bee.set_position(Vector2{0, 800});
|
||||||
|
|
||||||
|
Entity tree;
|
||||||
|
tree.set_texture("assets/graphics/tree.png");
|
||||||
|
tree.set_position(Vector2{810, 0});
|
||||||
|
|
||||||
|
Player player;
|
||||||
|
player.set_texture("assets/graphics/player.png");
|
||||||
|
player.set_position(Vector2{580, 720});
|
||||||
|
|
||||||
|
Entity gravestone;
|
||||||
|
gravestone.set_texture("assets/graphics/rip.png");
|
||||||
|
gravestone.set_position(Vector2{600, 860});
|
||||||
|
|
||||||
|
Entity axe;
|
||||||
|
axe.set_texture("assets/graphics/axe.png");
|
||||||
|
axe.set_position(Vector2{700, 830});
|
||||||
|
|
||||||
|
Log log;
|
||||||
|
log.set_texture("assets/graphics/log.png");
|
||||||
|
log.set_position(Vector2{810, 720});
|
||||||
|
|
||||||
|
const Vector2 AXE_POSITION{700, 1075};
|
||||||
|
|
||||||
|
sf::Clock clock;
|
||||||
|
|
||||||
|
sf::RectangleShape time_bar;
|
||||||
|
float time_bar_start_width = 400;
|
||||||
|
float time_bar_height = 80;
|
||||||
|
|
||||||
|
time_bar.setSize(sf::Vector2f(time_bar_start_width, time_bar_height));
|
||||||
|
time_bar.setFillColor(sf::Color::Red);
|
||||||
|
time_bar.setPosition((1920.0 / 2) - time_bar_start_width / 2, 980);
|
||||||
|
|
||||||
|
sf::Time game_time_total;
|
||||||
|
float time_remaining = 6.0f;
|
||||||
|
float time_bar_width_per_second = time_bar_start_width / time_remaining;
|
||||||
|
|
||||||
|
bool paused = true;
|
||||||
|
|
||||||
|
int score = 0;
|
||||||
|
|
||||||
|
Text message_text;
|
||||||
|
message_text.set_font("assets/fonts/KOMIKAP_.ttf");
|
||||||
|
message_text.set_string("Press Enter to start!");
|
||||||
|
message_text.set_size(75);
|
||||||
|
message_text.set_color(sf::Color::White);
|
||||||
|
|
||||||
|
Text score_text;
|
||||||
|
message_text.set_font("assets/fonts/KOMIKAP_.ttf");
|
||||||
|
message_text.set_string("Score: 0");
|
||||||
|
message_text.set_size(100);
|
||||||
|
message_text.set_color(sf::Color::White);
|
||||||
|
|
||||||
|
|
||||||
|
sf::FloatRect text_rect = message_text.get_rect();
|
||||||
|
message_text.text.setOrigin(text_rect.left + text_rect.width / 2.0f,
|
||||||
|
text_rect.top + text_rect.height / 2.0f);
|
||||||
|
|
||||||
|
message_text.set_position(Vector2{screen_size.x / 2.0f, screen_size.y / 2.0f});
|
||||||
|
score_text.set_position(Vector2{20, 20});
|
||||||
|
|
||||||
|
for (int i=0; i<NUM_CLOUDS; i++) {
|
||||||
|
clouds[i].set_texture("assets/graphics/cloud.png");
|
||||||
|
clouds[i].set_position(cloud_positions[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<NUM_BRANCHES; i++) {
|
||||||
|
branches[i].set_texture("assets/graphics/branch.png");
|
||||||
|
branches[i].set_position(Vector2{-2000, -2000});
|
||||||
|
branches[i].set_origin(Vector2{220, 20});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool accept_input = false;
|
||||||
|
|
||||||
|
sf::SoundBuffer chop_buff;
|
||||||
|
chop_buff.loadFromFile("assets/sound/chop.wav");
|
||||||
|
sf::Sound chop;
|
||||||
|
chop.setBuffer(chop_buff);
|
||||||
|
|
||||||
|
sf::SoundBuffer death_buff;
|
||||||
|
death_buff.loadFromFile("assets/sound/death.wav");
|
||||||
|
sf::Sound death;
|
||||||
|
death.setBuffer(death_buff);
|
||||||
|
|
||||||
|
sf::SoundBuffer oot_buff;
|
||||||
|
oot_buff.loadFromFile("assets/sound/out_of_time.wav");
|
||||||
|
sf::Sound oot;
|
||||||
|
oot.setBuffer(oot_buff);
|
||||||
|
|
||||||
|
while (window.isOpen()) {
|
||||||
|
// Handle input
|
||||||
|
|
||||||
|
sf::Event event;
|
||||||
|
|
||||||
|
while (window.pollEvent(event)) {
|
||||||
|
if (event.type == sf::Event::KeyReleased && !paused) {
|
||||||
|
accept_input = true;
|
||||||
|
|
||||||
|
axe.set_position(Vector2{2000, axe.get_position().y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
|
||||||
|
paused = false;
|
||||||
|
|
||||||
|
score = 0;
|
||||||
|
time_remaining = 6;
|
||||||
|
|
||||||
|
for (int i=1; i < NUM_BRANCHES; i++) {
|
||||||
|
branch_positions[i] = Side::none;
|
||||||
|
}
|
||||||
|
|
||||||
|
gravestone.set_position(Vector2{675, 2000});
|
||||||
|
|
||||||
|
player.set_position(Vector2{580, 720});
|
||||||
|
|
||||||
|
accept_input = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (accept_input) {
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
||||||
|
player.side = Side::right;
|
||||||
|
score++;
|
||||||
|
time_remaining += (2.0 / score) + .15;
|
||||||
|
|
||||||
|
axe.set_position(Vector2{AXE_POSITION.y, axe.get_position().y});
|
||||||
|
|
||||||
|
player.set_position(Vector2{1200, 720});
|
||||||
|
|
||||||
|
update_branches(score);
|
||||||
|
|
||||||
|
log.set_position(Vector2{810, 720});
|
||||||
|
log.speed_x = -5000;
|
||||||
|
log.active = true;
|
||||||
|
|
||||||
|
accept_input = false;
|
||||||
|
|
||||||
|
chop.play();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||||
|
player.side = Side::left;
|
||||||
|
score++;
|
||||||
|
time_remaining += (2.0 / score) + .15;
|
||||||
|
|
||||||
|
axe.set_position(Vector2{AXE_POSITION.x, axe.get_position().y});
|
||||||
|
|
||||||
|
player.set_position(Vector2{580, 720});
|
||||||
|
|
||||||
|
update_branches(score);
|
||||||
|
|
||||||
|
log.set_position(Vector2{810, 720});
|
||||||
|
log.speed_x = 5000;
|
||||||
|
log.active = true;
|
||||||
|
|
||||||
|
accept_input = false;
|
||||||
|
chop.play();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update scene
|
||||||
|
if (!paused) {
|
||||||
|
sf::Time delta = clock.restart();
|
||||||
|
|
||||||
|
time_remaining -= delta.asSeconds();
|
||||||
|
time_bar.setSize(sf::Vector2f(time_bar_width_per_second * time_remaining,
|
||||||
|
time_bar_height));
|
||||||
|
|
||||||
|
if (time_remaining <= 0.0f) {
|
||||||
|
paused = true;
|
||||||
|
message_text.set_string("Out of time!");
|
||||||
|
sf::FloatRect text_rect = message_text.get_rect();
|
||||||
|
message_text.set_origin(Vector2{text_rect.left + text_rect.width / 2.0f,
|
||||||
|
text_rect.top + text_rect.height / 2.0f});
|
||||||
|
message_text.set_position(Vector2{1920 / 2.0f, 1080 / 2.0f});
|
||||||
|
oot.play();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bee.active) {
|
||||||
|
srand((int)time(0));
|
||||||
|
bee.speed = (rand() % 200) + 200;
|
||||||
|
|
||||||
|
srand((int)time(0) * 10);
|
||||||
|
float height = (rand() % 500) + 500;
|
||||||
|
bee.set_position(Vector2{2000, height});
|
||||||
|
bee.active = true;
|
||||||
|
} else {
|
||||||
|
Vector2 pos = bee.get_position();
|
||||||
|
bee.set_position(Vector2{pos.x - (bee.speed * delta.asSeconds()), pos.y});
|
||||||
|
|
||||||
|
if (bee.get_position().x < -100) {
|
||||||
|
bee.active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!clouds[0].active) {
|
||||||
|
srand((int)time(0) * 10);
|
||||||
|
clouds[0].speed = (rand() % 200);
|
||||||
|
|
||||||
|
srand((int)time(0) * 10);
|
||||||
|
float height = (rand() % 150);
|
||||||
|
clouds[0].set_position(Vector2{-200, height});
|
||||||
|
clouds[0].active = true;
|
||||||
|
} else {
|
||||||
|
clouds[0].set_position(Vector2{clouds[0].get_position().x +
|
||||||
|
(clouds[0].speed * delta.asSeconds()),
|
||||||
|
clouds[0].get_position().y});
|
||||||
|
|
||||||
|
if (clouds[0].get_position().x > 1920) {
|
||||||
|
clouds[0].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!clouds[1].active) {
|
||||||
|
srand((int)time(0) * 20);
|
||||||
|
clouds[1].speed = (rand() % 200);
|
||||||
|
|
||||||
|
srand((int)time(0) * 20);
|
||||||
|
float height = (rand() % 300) - 150;
|
||||||
|
clouds[1].set_position(Vector2{-200, height});
|
||||||
|
clouds[1].active = true;
|
||||||
|
} else {
|
||||||
|
clouds[1].set_position(Vector2{clouds[1].get_position().x +
|
||||||
|
(clouds[1].speed * delta.asSeconds()),
|
||||||
|
clouds[1].get_position().y});
|
||||||
|
|
||||||
|
if (clouds[1].get_position().x > 1920) {
|
||||||
|
clouds[1].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!clouds[2].active) {
|
||||||
|
srand((int)time(0) * 30);
|
||||||
|
clouds[2].speed = (rand() % 200);
|
||||||
|
|
||||||
|
srand((int)time(0) * 30);
|
||||||
|
float height = (rand() % 450) - 150;
|
||||||
|
clouds[2].set_position(Vector2{-200, height});
|
||||||
|
clouds[2].active = true;
|
||||||
|
} else {
|
||||||
|
clouds[2].set_position(Vector2{clouds[2].get_position().x +
|
||||||
|
(clouds[2].speed * delta.asSeconds()),
|
||||||
|
clouds[2].get_position().y});
|
||||||
|
|
||||||
|
if (clouds[2].get_position().x > 1920) {
|
||||||
|
clouds[2].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Score: " << score;
|
||||||
|
score_text.set_string(ss.str());
|
||||||
|
|
||||||
|
for (int i=0; i<NUM_BRANCHES; i++) {
|
||||||
|
float height = i * 150;
|
||||||
|
|
||||||
|
if (branch_positions[i] == Side::left) {
|
||||||
|
branches[i].set_position(Vector2{610, height});
|
||||||
|
|
||||||
|
branches[i].set_rotation(180);
|
||||||
|
} else if (branch_positions[i] == Side::right) {
|
||||||
|
branches[i].set_position(Vector2{1330, height});
|
||||||
|
|
||||||
|
branches[i].set_rotation(0);
|
||||||
|
} else {
|
||||||
|
branches[i].set_position(Vector2{3000, height});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.active) {
|
||||||
|
log.set_position(Vector2{log.get_position().x + (log.speed_x * delta.asSeconds()),
|
||||||
|
log.get_position().y + (log.speed_y * delta.asSeconds())});
|
||||||
|
|
||||||
|
if (log.get_position().x < -100 || log.get_position().x > 2000) {
|
||||||
|
log.active = false;
|
||||||
|
log.set_position(Vector2{810, 720});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (branch_positions[5] == player.side) {
|
||||||
|
paused = true;
|
||||||
|
accept_input = false;
|
||||||
|
|
||||||
|
gravestone.set_position(Vector2{525, 760});
|
||||||
|
|
||||||
|
player.set_position(Vector2{2000, 660});
|
||||||
|
|
||||||
|
message_text.set_string("SQUISHED!");
|
||||||
|
|
||||||
|
sf::FloatRect text_rect = message_text.get_rect();
|
||||||
|
message_text.set_origin(Vector2{text_rect.left + text_rect.width / 2.0f,
|
||||||
|
text_rect.top + text_rect.height / 2.0f});
|
||||||
|
|
||||||
|
message_text.set_position(Vector2{1920 / 2.0f, 1080 / 2.0f});
|
||||||
|
death.play();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the scene
|
||||||
|
|
||||||
|
// Clear everything from last frame
|
||||||
|
window.clear();
|
||||||
|
|
||||||
|
// Draw game scene here
|
||||||
|
draw(&window, &bg);
|
||||||
|
|
||||||
|
for (int i=0; i<NUM_CLOUDS; i++) {
|
||||||
|
draw(&window, &clouds[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<NUM_BRANCHES; i++) {
|
||||||
|
draw(&window, &branches[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
draw(&window, &tree);
|
||||||
|
draw(&window, &player);
|
||||||
|
draw(&window, &axe);
|
||||||
|
draw(&window, &log);
|
||||||
|
|
||||||
|
draw(&window, &bee);
|
||||||
|
|
||||||
|
draw(&window, &gravestone);
|
||||||
|
|
||||||
|
draw(&window, &score_text);
|
||||||
|
|
||||||
|
window.draw(time_bar);
|
||||||
|
|
||||||
|
if (paused) {
|
||||||
|
draw(&window, &message_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show everything we just drew
|
||||||
|
window.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_branches(int seed) {
|
||||||
|
for (int j=NUM_BRANCHES; j>0; j--) {
|
||||||
|
branch_positions[j] = branch_positions[j-1];
|
||||||
|
}
|
||||||
|
|
||||||
|
srand((int)time(0)+seed);
|
||||||
|
int r = (rand() % 5);
|
||||||
|
|
||||||
|
switch(r) {
|
||||||
|
case 0:
|
||||||
|
branch_positions[0] = Side::left;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
branch_positions[1] = Side::right;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
branch_positions[2] = Side::none;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
zombie_shooter/.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
assets/fonts/Rajdhani/*.ttf filter=lfs diff=lfs merge=lfs -text
|
||||||
32
zombie_shooter/.gitignore
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
*.smod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
14
zombie_shooter/Makefile
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
CXX=g++
|
||||||
|
CXXFLAGS=-Wall -g -std=c++11 -I/opt/sfml2/include
|
||||||
|
SFML_LIBS=-L/opt/sfml2/lib -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
|
||||||
|
|
||||||
|
all: zombie_shooter
|
||||||
|
|
||||||
|
zombie_shooter: src/main.o src/core.o
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $^ $(SFML_LIBS)
|
||||||
|
|
||||||
|
%.o: %.cpp %.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f zombie_shooter *.o
|
||||||
BIN
zombie_shooter/assets/fonts/Rajdhani/Rajdhani-Bold.ttf
(Stored with Git LFS)
Normal file
BIN
zombie_shooter/assets/fonts/Rajdhani/Rajdhani-Light.ttf
(Stored with Git LFS)
Normal file
BIN
zombie_shooter/assets/fonts/Rajdhani/Rajdhani-Medium.ttf
(Stored with Git LFS)
Normal file
BIN
zombie_shooter/assets/fonts/Rajdhani/Rajdhani-Regular.ttf
(Stored with Git LFS)
Normal file
BIN
zombie_shooter/assets/fonts/Rajdhani/Rajdhani-SemiBold.ttf
(Stored with Git LFS)
Normal file
BIN
zombie_shooter/assets/graphics/ammo_icon.png
Normal file
|
After Width: | Height: | Size: 567 B |
BIN
zombie_shooter/assets/graphics/ammo_pickup.png
Normal file
|
After Width: | Height: | Size: 567 B |
BIN
zombie_shooter/assets/graphics/background.png
Normal file
|
After Width: | Height: | Size: 42 KiB |
BIN
zombie_shooter/assets/graphics/background_sheet.png
Normal file
|
After Width: | Height: | Size: 5.3 KiB |
BIN
zombie_shooter/assets/graphics/bloater.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
zombie_shooter/assets/graphics/blood.png
Normal file
|
After Width: | Height: | Size: 725 B |
BIN
zombie_shooter/assets/graphics/chaser.png
Normal file
|
After Width: | Height: | Size: 889 B |
BIN
zombie_shooter/assets/graphics/crawler.png
Normal file
|
After Width: | Height: | Size: 905 B |
BIN
zombie_shooter/assets/graphics/crosshair.png
Normal file
|
After Width: | Height: | Size: 691 B |
BIN
zombie_shooter/assets/graphics/health_pickup.png
Normal file
|
After Width: | Height: | Size: 574 B |
BIN
zombie_shooter/assets/graphics/player.png
Normal file
|
After Width: | Height: | Size: 1002 B |
BIN
zombie_shooter/assets/graphics/sample.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
zombie_shooter/assets/sound/hit.wav
Normal file
BIN
zombie_shooter/assets/sound/pickup.wav
Normal file
BIN
zombie_shooter/assets/sound/powerup.wav
Normal file
BIN
zombie_shooter/assets/sound/reload.wav
Normal file
BIN
zombie_shooter/assets/sound/reload_failed.wav
Normal file
BIN
zombie_shooter/assets/sound/shoot.wav
Normal file
BIN
zombie_shooter/assets/sound/splat.wav
Normal file
54
zombie_shooter/include/core.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
struct Entity {
|
||||||
|
sf::Vector2f position;
|
||||||
|
float speed;
|
||||||
|
sf::Vector2f direction;
|
||||||
|
sf::Vector2f velocity;
|
||||||
|
|
||||||
|
virtual void update(sf::Time delta) = 0;
|
||||||
|
void set_position(sf::Vector2f new_pos);
|
||||||
|
void set_size(sf::Vector2f new_size);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Player {
|
||||||
|
const float START_SPEED = 200;
|
||||||
|
const float START_HEALTH = 100;
|
||||||
|
|
||||||
|
sf::Vector2f position;
|
||||||
|
sf::Sprite sprite;
|
||||||
|
sf::Texture texture;
|
||||||
|
sf::Vector2f resolution;
|
||||||
|
sf::IntRect arena;
|
||||||
|
int tile_size;
|
||||||
|
|
||||||
|
bool up_pressed;
|
||||||
|
bool down_pressed;
|
||||||
|
bool left_pressed;
|
||||||
|
bool right_pressed;
|
||||||
|
|
||||||
|
int health;
|
||||||
|
int max_health;
|
||||||
|
sf::Time last_hit;
|
||||||
|
float speed;
|
||||||
|
|
||||||
|
Player();
|
||||||
|
|
||||||
|
void spawn(sf::IntRect arena, sf::Vector2f resolution, int tile_size);
|
||||||
|
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
bool hit(sf::Time time_hit);
|
||||||
|
|
||||||
|
void update(float delta, sf::Vector2i mouse_position);
|
||||||
|
|
||||||
|
void upgrade_speed();
|
||||||
|
|
||||||
|
void upgrade_health();
|
||||||
|
|
||||||
|
void increase_health_level(int amount);
|
||||||
|
};
|
||||||
3
zombie_shooter/run
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/zsh
|
||||||
|
|
||||||
|
export LD_LIBRARY_PATH=/opt/sfml2/lib && ./zombie_shooter
|
||||||
115
zombie_shooter/src/core.cpp
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
#include "../include/core.h"
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
void Entity::set_position(sf::Vector2f new_pos) {
|
||||||
|
position = new_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::set_size(sf::Vector2f new_size) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Player::Player()
|
||||||
|
: speed(START_SPEED),
|
||||||
|
health(START_HEALTH),
|
||||||
|
max_health(START_HEALTH),
|
||||||
|
texture(),
|
||||||
|
sprite()
|
||||||
|
{
|
||||||
|
texture.loadFromFile("./assets/graphics/player.png");
|
||||||
|
sprite.setTexture(texture);
|
||||||
|
sprite.setOrigin(sf::Vector2f(25, 25));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::spawn(sf::IntRect arena, sf::Vector2f resolution, int tile_size) {
|
||||||
|
// Place in the middle of the arena
|
||||||
|
position.x = arena.width / 2.0;
|
||||||
|
position.y = arena.height / 2.0;
|
||||||
|
|
||||||
|
arena.left = arena.left;
|
||||||
|
arena.width = arena.width;
|
||||||
|
arena.top = arena.top;
|
||||||
|
arena.height = arena.height;
|
||||||
|
|
||||||
|
tile_size = tile_size;
|
||||||
|
|
||||||
|
resolution.x = resolution.x;
|
||||||
|
resolution.y = resolution.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::reset() {
|
||||||
|
speed = START_SPEED;
|
||||||
|
health = START_HEALTH;
|
||||||
|
max_health = START_HEALTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Player::hit(sf::Time time_hit) {
|
||||||
|
bool is_hit = time_hit.asMilliseconds() - last_hit.asMilliseconds() > 200;
|
||||||
|
|
||||||
|
if (is_hit) {
|
||||||
|
last_hit = time_hit;
|
||||||
|
health -= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return is_hit;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Player::update(float delta, sf::Vector2i mouse_position) {
|
||||||
|
if (up_pressed) {
|
||||||
|
position.y -= speed * delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (down_pressed) {
|
||||||
|
position.y += speed * delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (right_pressed) {
|
||||||
|
position.x += speed * delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left_pressed) {
|
||||||
|
position.x -= speed * delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
sprite.setPosition(position);
|
||||||
|
|
||||||
|
if (position.x > arena.width - tile_size) {
|
||||||
|
position.x = arena.width - tile_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position.x < arena.width + tile_size) {
|
||||||
|
position.x = arena.width + tile_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position.y > arena.height - tile_size) {
|
||||||
|
position.y = arena.height - tile_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position.y < arena.height + tile_size) {
|
||||||
|
position.y = arena.height + tile_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
float angle = (std::atan2(mouse_position.y - resolution.y / 2, mouse_position.x - resolution.x / 2) * 180 / 3.141);
|
||||||
|
sprite.setRotation(angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::upgrade_speed() {
|
||||||
|
speed += (START_SPEED * 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::upgrade_health() {
|
||||||
|
max_health += (START_HEALTH * 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::increase_health_level(int amount) {
|
||||||
|
health += amount;
|
||||||
|
|
||||||
|
if (health > max_health) {
|
||||||
|
health = max_health;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
199
zombie_shooter/src/main.cpp
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
#include "../include/core.h"
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
#include <SFML/Window/WindowStyle.hpp>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <SFML/Audio/SoundBuffer.hpp>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <SFML/Audio.hpp>
|
||||||
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <SFML/Window/VideoMode.hpp>
|
||||||
|
|
||||||
|
const int MAX_ENTITIES = 2;
|
||||||
|
Entity *entities[MAX_ENTITIES];
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// sf::Text hud;
|
||||||
|
// sf::Font font;
|
||||||
|
// font.loadFromFile("assets/fonts/Rajdhani/Rajdhani-Regular.ttf");
|
||||||
|
// hud.setFont(font);
|
||||||
|
// hud.setCharacterSize(75);
|
||||||
|
// hud.setFillColor(sf::Color::White);
|
||||||
|
// hud.setPosition(20, 20);
|
||||||
|
|
||||||
|
enum State { PAUSED, LEVELING_UP, GAME_OVER, PLAYING };
|
||||||
|
|
||||||
|
State state = State::GAME_OVER;
|
||||||
|
|
||||||
|
sf::Vector2f resolution;
|
||||||
|
resolution.x = sf::VideoMode::getDesktopMode().width;
|
||||||
|
resolution.y = sf::VideoMode::getDesktopMode().height;
|
||||||
|
|
||||||
|
sf::VideoMode vm(resolution.x, resolution.y);
|
||||||
|
sf::RenderWindow window(vm, "Zombie Shooter", sf::Style::Fullscreen);
|
||||||
|
|
||||||
|
sf::View main_view(sf::FloatRect(0, 0, resolution.x, resolution.y));
|
||||||
|
|
||||||
|
sf::Clock clock;
|
||||||
|
sf::Time game_time_total;
|
||||||
|
sf::Vector2f mouse_world_position;
|
||||||
|
sf::Vector2i mouse_screen_position;
|
||||||
|
|
||||||
|
Player player;
|
||||||
|
|
||||||
|
sf::IntRect arena;
|
||||||
|
|
||||||
|
|
||||||
|
while (window.isOpen()) {
|
||||||
|
// INPUT
|
||||||
|
{
|
||||||
|
sf::Event event;
|
||||||
|
while (window.pollEvent(event)) {
|
||||||
|
// if (event.type == sf::Event::Closed) {
|
||||||
|
// window.close();
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (event.type == sf::Event::KeyPressed) {
|
||||||
|
if (event.key.code == sf::Keyboard::Return && state == State::PLAYING) {
|
||||||
|
state = State::PAUSED;
|
||||||
|
} else if (event.key.code == sf::Keyboard::Return && state == State::PAUSED) {
|
||||||
|
state = State::PLAYING;
|
||||||
|
// Restart clock so there isn't a frame jump
|
||||||
|
clock.restart();
|
||||||
|
} else if (event.key.code == sf::Keyboard::Return && state == State::GAME_OVER) {
|
||||||
|
state = State::LEVELING_UP;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == State::PLAYING) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player movement
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
||||||
|
player.up_pressed = true;
|
||||||
|
} else {
|
||||||
|
player.up_pressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
||||||
|
player.left_pressed = true;
|
||||||
|
} else {
|
||||||
|
player.left_pressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
||||||
|
player.down_pressed = true;
|
||||||
|
} else {
|
||||||
|
player.down_pressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
||||||
|
player.right_pressed = true;
|
||||||
|
} else {
|
||||||
|
player.right_pressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// LEVELING_UP
|
||||||
|
if (state == State::LEVELING_UP) {
|
||||||
|
if (event.key.code == sf::Keyboard::Num1) {
|
||||||
|
state = State::PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key.code == sf::Keyboard::Num2) {
|
||||||
|
state = State::PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key.code == sf::Keyboard::Num3) {
|
||||||
|
state = State::PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key.code == sf::Keyboard::Num4) {
|
||||||
|
state = State::PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key.code == sf::Keyboard::Num5) {
|
||||||
|
state = State::PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key.code == sf::Keyboard::Num6) {
|
||||||
|
state = State::PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == State::PLAYING) {
|
||||||
|
arena.width = 500;
|
||||||
|
arena.height = 500;
|
||||||
|
arena.left = 0;
|
||||||
|
arena.top = 0;
|
||||||
|
int tile_size = 50;
|
||||||
|
player.spawn(arena, resolution, tile_size);
|
||||||
|
|
||||||
|
// Restart to avoid frame jump
|
||||||
|
clock.restart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// UPDATE
|
||||||
|
{
|
||||||
|
if (state == State::PLAYING) {
|
||||||
|
sf::Time delta = clock.restart();
|
||||||
|
|
||||||
|
game_time_total += delta;
|
||||||
|
|
||||||
|
float delta_as_seconds = delta.asSeconds();
|
||||||
|
|
||||||
|
mouse_screen_position = sf::Mouse::getPosition();
|
||||||
|
mouse_world_position = window.mapPixelToCoords(mouse_screen_position, main_view);
|
||||||
|
|
||||||
|
player.update(delta_as_seconds, mouse_screen_position);
|
||||||
|
|
||||||
|
// sf::Vector2f player_position(player.position);
|
||||||
|
|
||||||
|
main_view.setCenter(player.position);
|
||||||
|
}
|
||||||
|
|
||||||
|
// for (int i = 0; i < MAX_ENTITIES; i++) {
|
||||||
|
// entities[i]->update(delta);
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// DRAW
|
||||||
|
{
|
||||||
|
if (state == State::PLAYING) {
|
||||||
|
window.clear();
|
||||||
|
window.setView(main_view);
|
||||||
|
window.draw(player.sprite);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == State::LEVELING_UP) {
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == State::PAUSED) {
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == State::GAME_OVER) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// for (int i = 0; i < MAX_ENTITIES; i++) {
|
||||||
|
// window.draw(entities[i]->shape);
|
||||||
|
// }
|
||||||
|
|
||||||
|
window.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||