Skip to content

Commit

Permalink
Part 2 source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kofybrek authored Feb 21, 2022
1 parent f4c895c commit 081ee53
Show file tree
Hide file tree
Showing 43 changed files with 1,865 additions and 307 deletions.
27 changes: 18 additions & 9 deletions Source/Animation.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//I didn't use the constants from "Global.hpp" to make this class more universal.
#include <SFML/Graphics.hpp>

#include "Headers/Animation.hpp"

Animation::Animation(unsigned short i_animation_speed, unsigned short i_frame_width, const std::string& i_texture_location) :
Animation::Animation(const unsigned short i_frame_width, const std::string& i_texture_location, const unsigned short i_animation_speed) :
flipped(0),
animation_iterator(0),
animation_speed(std::max<unsigned short>(1, i_animation_speed)),
Expand All @@ -15,9 +14,8 @@ Animation::Animation(unsigned short i_animation_speed, unsigned short i_frame_wi
total_frames = texture.getSize().x / frame_width;
}

void Animation::draw(short i_x, short i_y, sf::RenderWindow& i_window)
void Animation::draw(sf::RenderWindow& i_window)
{
sprite.setPosition(i_x, i_y);
sprite.setTexture(texture);

if (0 == flipped)
Expand All @@ -26,31 +24,42 @@ void Animation::draw(short i_x, short i_y, sf::RenderWindow& i_window)
}
else
{
//To flip the sprite, we're gonna read the texture from right to left.
//This is why I love SFML.
//It allows you to read the texture from right to left using negative numbers.
sprite.setTextureRect(sf::IntRect(frame_width * (1 + current_frame), 0, -frame_width, texture.getSize().y));
}

i_window.draw(sprite);
}

void Animation::set_animation_speed(unsigned short i_animation_speed)
void Animation::set_animation_speed(const unsigned short i_animation_speed)
{
animation_speed = std::max<unsigned short>(1, i_animation_speed);
}

void Animation::set_flipped(bool i_value)
void Animation::set_flipped(const bool i_value)
{
flipped = i_value;
}

void Animation::update()
void Animation::set_position(const short i_x, const short i_y)
{
animation_iterator++;
sprite.setPosition(i_x, i_y);
}

void Animation::set_texture_location(const std::string& i_texture_location)
{
texture.loadFromFile(i_texture_location);
}

void Animation::update()
{
while (animation_iterator >= animation_speed)
{
animation_iterator -= animation_speed;

current_frame = (1 + current_frame) % total_frames;
}

animation_iterator++;
}
61 changes: 40 additions & 21 deletions Source/ConvertSketch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,82 @@

#include "Headers/Animation.hpp"
#include "Headers/Global.hpp"
#include "Headers/MapManager.hpp"
#include "Headers/Mushroom.hpp"
#include "Headers/Mario.hpp"
#include "Headers/Enemy.hpp"
#include "Headers/Goomba.hpp"
#include "Headers/Koopa.hpp"
#include "Headers/ConvertSketch.hpp"

Map convert_sketch(std::vector<Goomba>& i_goombas, const sf::Image& i_map_sketch, Mario& i_mario)
//One person asked, "Why don't you use Tiled Map Editor?"
//My answer is, "Why should I work hard, when I don't have to work hard?"
void convert_sketch(const unsigned char i_current_level, unsigned short& i_level_finish, std::vector<std::shared_ptr<Enemy>>& i_enemies, sf::Color& i_background_color, MapManager& i_map_manager, Mario& i_mario)
{
//The map sketch is divided into 3 sections:
//1) Placement of all the important blocks (walls, bricks, question blocks, etc.).
//2) Placement of entities.
//3) Placement of background tiles.
unsigned short map_height = floor(i_map_sketch.getSize().y / 3.f);
unsigned short map_height;

Map output_map(i_map_sketch.getSize().x);
i_map_manager.update_map_sketch(i_current_level);
i_map_manager.set_map_size(i_map_manager.get_map_sketch_width());

for (unsigned short a = 0; a < i_map_sketch.getSize().x; a++)
//We divide the height by 3 because the sketch stores the level as 3 layers: blocks, entities, and background tiles.
map_height = floor(i_map_manager.get_map_sketch_height() / 3.f);

i_background_color = i_map_manager.get_map_sketch_pixel(0, i_map_manager.get_map_sketch_height() - 1);

for (unsigned short a = 0; a < i_map_manager.get_map_sketch_width(); a++)
{
for (unsigned short b = 0; b < 2 * map_height; b++)
{
sf::Color pixel = i_map_sketch.getPixel(a, b);
sf::Color pixel = i_map_manager.get_map_sketch_pixel(a, b);

//First we're gonna place the blocks.
if (b < map_height)
{
if (sf::Color(182, 73, 0) == pixel)
{
output_map[a][b] = Cell::Brick;
i_map_manager.set_map_cell(a, b, Cell::Brick);
}
else if (sf::Color(255, 255, 0) == pixel)
{
i_map_manager.set_map_cell(a, b, Cell::Coin);
}
else if (sf::Color(0, 182, 0) == pixel)
else if (sf::Color(0, 146, 0) == pixel || sf::Color(0, 182, 0) == pixel || sf::Color(0, 219, 0) == pixel)
{
output_map[a][b] = Cell::Pipe;
//Multiple colors, because we need to know which part of the pipe we need to draw.
i_map_manager.set_map_cell(a, b, Cell::Pipe);
}
else if (sf::Color(255, 146, 85) == pixel)
else if (sf::Color(255, 73, 85) == pixel || sf::Color(255, 146, 85) == pixel)
{
output_map[a][b] = Cell::QuestionBlock;
i_map_manager.set_map_cell(a, b, Cell::QuestionBlock);
}
else if (sf::Color(0, 0, 0) == pixel || sf::Color(146, 73, 0) == pixel)
{
output_map[a][b] = Cell::Wall;
i_map_manager.set_map_cell(a, b, Cell::Wall);
}
else
{
output_map[a][b] = Cell::Empty;
i_map_manager.set_map_cell(a, b, Cell::Empty);

if (sf::Color(0, 255, 255) == pixel)
{
i_level_finish = a;
}
}
}
else //Then we're gonna place the entities.
else
{
if (sf::Color(255, 0, 0) == pixel)
{
i_mario.set_position(CELL_SIZE * a, CELL_SIZE * (b - map_height));
}
else if (sf::Color(182, 73, 0) == pixel)
{
i_goombas.push_back(Goomba(CELL_SIZE * a, CELL_SIZE * (b - map_height)));
i_enemies.push_back(std::make_shared<Goomba>(sf::Color(0, 0, 85) == i_background_color, CELL_SIZE * a, CELL_SIZE * (b - map_height)));
}
else if (sf::Color(0, 219, 0) == pixel)
{
i_enemies.push_back(std::make_shared<Koopa>(sf::Color(0, 0, 85) == i_background_color, CELL_SIZE * a, CELL_SIZE * (b - map_height)));
}
}
}
}

return output_map;
}
35 changes: 35 additions & 0 deletions Source/Enemy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <array>
#include <chrono>
#include <SFML/Graphics.hpp>

#include "Headers/Animation.hpp"
#include "Headers/Global.hpp"
#include "Headers/MapManager.hpp"
#include "Headers/Mushroom.hpp"
#include "Headers/Mario.hpp"
#include "Headers/Enemy.hpp"

Enemy::Enemy(const float i_x, const float i_y) :
dead(0),
horizontal_speed(0),
vertical_speed(0),
x(i_x),
y(i_y)
{

}

bool Enemy::get_dead(const bool i_deletion) const
{
return dead;
}

void Enemy::die(const unsigned char i_death_type)
{
dead = 1;
}

sf::FloatRect Enemy::get_hit_box() const
{
return sf::FloatRect(x, y, CELL_SIZE, CELL_SIZE);
}
Loading

0 comments on commit 081ee53

Please sign in to comment.