Skip to content

Commit

Permalink
Load and Init entity::Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
copyrat90 committed May 30, 2021
1 parent 10079f2 commit d678774
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 47 deletions.
20 changes: 18 additions & 2 deletions include/game_entity_Entity.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
#pragma once

#include <bn_fixed_rect.h>
#include <bn_optional.h>
#include <bn_sprite_item.h>
#include <bn_sprite_ptr.h>

namespace sym::game::entity
{

class Entity
{
public:
virtual void FreeGraphicResources() = 0;
virtual void AllocateGraphicResources() = 0;
virtual ~Entity() = 0;

Entity(bn::fixed_point position, bn::fixed_rect collider, const bn::sprite_item* const spriteItem = nullptr);
Entity(Entity&& other) noexcept;

void FreeGraphicResource();
virtual void AllocateGraphicResource();

protected:
bn::fixed_point position_;
bn::fixed_rect collider_;
bn::optional<bn::sprite_ptr> sprite_;
const bn::sprite_item* const spriteItem_;
};

} // namespace sym::game::entity
30 changes: 24 additions & 6 deletions include/game_entity_Symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@

#include "game_entity_Entity.h"

#include <bn_optional.h>
#include <bn_sprite_ptr.h>

namespace sym::game::entity
{

class Symbol final : Entity
{
public:
void FreeGraphicResources() final;
void AllocateGraphicResources() final;
static constexpr int COMPLEX_SYMBOL_START_NUM = 100;
/**
* @brief Symbol type
*
* If `[enum value] >= COMPLEX_SYMBOL_START_NUM`, it is a complex symbol.
* otherwise, it is a basic symbol.
*/
enum Type
{
BAR,
XOR,
UP = COMPLEX_SYMBOL_START_NUM,
VV,
PLUS
};

Symbol(bn::fixed_point position, Symbol::Type type);
Symbol(Symbol&& other) noexcept;

void AllocateGraphicResource() final;

Type GetType() const;
void SetType(Type);

private:
bn::optional<bn::sprite_ptr> sprite_;
Symbol::Type type_;
};

} // namespace sym::game::entity
54 changes: 26 additions & 28 deletions include/game_stage_ZoneInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#include <bn_affine_bg_item.h>
#include <bn_assert.h>
#include <bn_display.h>
#include <bn_fixed_point.h>
#include <bn_fixed_rect.h>
#include <bn_span.h>

#include "helper_tilemap.h"

#include "game_entity_Symbol.h"

namespace sym::game::stage
{

Expand All @@ -21,23 +22,8 @@ struct ZoneInfo
{
struct SymbolInfo
{
/**
* @brief Symbol types
*
* If value >= 100, it is a complex symbol.
* otherwise, it is a basic symbol.
*/
enum Type
{
BAR,
XOR,
UP = 100,
VV,
PLUS
};

bn::fixed_point position;
Type symbolType;
entity::Symbol::Type symbolType;
};

struct ButtonInfo
Expand All @@ -61,32 +47,44 @@ struct ZoneInfo
int entranceOfZoneIndex;
};

struct ShutterInfo
{
bn::fixed_point position;
};

struct EntranceInfo
{
bn::fixed_point position;
};

/**
* @brief Constructor.
*
* @param zoneBoundary_a anchor is center of bg
*/
constexpr ZoneInfo(const bn::affine_bg_item& mapBg_a, helper::tilemap::IndexRect zoneBoundary_a,
bn::span<SymbolInfo> symbols_a, bn::span<ButtonInfo> hoverButtons_a,
bn::span<ButtonInfo> pressureButtons_a, bn::span<DoorInfo> doors_a, bn::span<ExitInfo> exits_a,
bn::span<bn::fixed_point> entrances_a)
constexpr ZoneInfo(const bn::affine_bg_item& mapBg_a, const helper::tilemap::IndexRect zoneBoundary_a,
bn::span<const SymbolInfo> symbols_a, bn::span<const ButtonInfo> hoverButtons_a,
bn::span<const ButtonInfo> pressureButtons_a, bn::span<const DoorInfo> doors_a,
bn::span<const ExitInfo> exits_a, bn::span<const ShutterInfo> shutters_a,
bn::span<const EntranceInfo> entrances_a)
: mapBg(mapBg_a), symbols(symbols_a), hoverButtons(hoverButtons_a), pressureButtons(pressureButtons_a),
zoneBoundary(zoneBoundary_a), doors(doors_a), exits(exits_a), entrances(entrances_a)
zoneBoundary(zoneBoundary_a), doors(doors_a), exits(exits_a), shutters(shutters_a), entrances(entrances_a)
{
}

const bn::affine_bg_item& mapBg;

// Mutable things. Only for initialize.
const bn::span<SymbolInfo> symbols;
const bn::span<ButtonInfo> hoverButtons;
const bn::span<ButtonInfo> pressureButtons;
const bn::span<const SymbolInfo> symbols;
const bn::span<const ButtonInfo> hoverButtons;
const bn::span<const ButtonInfo> pressureButtons;

// Constant things. Constantly referenced.
const bn::fixed_rect zoneBoundary;
const bn::span<DoorInfo> doors;
const bn::span<ExitInfo> exits;
const bn::span<bn::fixed_point> entrances;
const bn::span<const DoorInfo> doors;
const bn::span<const ExitInfo> exits;
const bn::span<const ShutterInfo> shutters;
const bn::span<const EntranceInfo> entrances;
};

} // namespace sym::game::stage
19 changes: 19 additions & 0 deletions include/helper_rect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bn_fixed_rect.h>

namespace sym::helper::rect
{

[[nodiscard]] constexpr bn::fixed_rect MakeFixedRectByTopLeftAndSize(bn::fixed_point topLeft, bn::fixed_size size)
{
const bn::fixed_point centerPos = topLeft + bn::fixed_point(size.width() / 2, size.height() / 2);
return bn::fixed_rect(centerPos, size);
}

[[nodiscard]] constexpr bn::fixed_rect MakeFixedRectByTopLeftAndBottomRightPosition(bn::fixed_point topLeft,
bn::fixed_point bottomRight)
{
const bn::fixed_size size(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y());
return MakeFixedRectByTopLeftAndSize(topLeft, size);
}

} // namespace sym::helper::rect
6 changes: 3 additions & 3 deletions include/helper_tilemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <bn_fixed_rect.h>
#include <bn_size.h>

#include "helper_rect.h"

namespace sym::helper::tilemap
{

Expand All @@ -17,9 +19,7 @@ using IndexPoint = bn::fixed_point;

[[nodiscard]] constexpr IndexRect MakeIndexRectByTopLeftAndBottomRight(IndexPoint topLeft, IndexPoint bottomRight)
{
const bn::fixed_size size(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y());
const IndexPoint centerPos = topLeft + IndexPoint(size.width() / 2, size.height() / 2);
return {centerPos, size};
return rect::MakeFixedRectByTopLeftAndBottomRightPosition(topLeft, bottomRight);
}

/**
Expand Down
38 changes: 37 additions & 1 deletion include/scene_Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

#include "scene_IScene.h"

#include <bn_affine_bg_ptr.h>
#include <bn_array.h>
#include <bn_fixed_point.h>
#include <bn_forward_list.h>
#include <bn_memory.h>
#include <bn_optional.h>
#include <bn_span.h>
#include <bn_vector.h>

#include "effect_Transition.h"
#include "game_Status.h"
#include "game_entity_Symbol.h"
#include "game_stage_Id.h"
#include "game_stage_StageInfo.h"

Expand All @@ -17,17 +24,46 @@ class Game final : public IScene
{
public:
Game(game::Status& status);
~Game();

[[nodiscard]] bn::optional<Type> Update() final;

private:
static constexpr int FADE_IN_UPDATE_COUNT = 30;
static constexpr int FADE_OUT_UPDATE_COUNT = 30;

game::Status status_;
static constexpr int ZONE_MAX_COUNT = 8;
static constexpr int STAGE_SYMBOL_MAX_COUNT = 16;
static constexpr int ZONE_HOVER_BUTTON_MAX_COUNT = 4;
static constexpr int ZONE_PRESSURE_BUTTON_MAX_COUNT = 4;
static constexpr int ZONE_DOOR_MAX_COUNT = 4;
static constexpr int ZONE_EXIT_MAX_COUNT = 4;
static constexpr int ZONE_SHUTTER_MAX_COUNT = 4;
static constexpr int ZONE_ENTRANCE_MAX_COUNT = ZONE_DOOR_MAX_COUNT + ZONE_EXIT_MAX_COUNT;

game::Status& status_;
const game::stage::StageInfo& stageInfo_;

effect::Transition fadeIn_;
effect::Transition fadeOut_;

int currentZoneIdx_;
bn::affine_bg_ptr currentMapBg_;

// Movable entities.
bn::vector<bn::forward_list<game::entity::Symbol, STAGE_SYMBOL_MAX_COUNT>, ZONE_MAX_COUNT> symbolsOfZones_;
bn::array<bn::optional<game::entity::Symbol>, 2> symbolsInHands_;

// Fixed entities.
// bn::vector<bn::vector<game::entity::HoverButton, ZONE_HOVER_BUTTON_MAX_COUNT>, ZONE_MAX_COUNT>
// hoverButtonsOfZones_; bn::vector<bn::vector<game::entity::PressureButton, ZONE_PRESSURE_BUTTON_MAX_COUNT>,
// ZONE_MAX_COUNT> pressureButtonsOfZones_; bn::vector<bn::vector<game::entity::Door, ZONE_DOOR_MAX_COUNT>,
// ZONE_MAX_COUNT> doorsOfZones_;
// bn::vector<bn::vector<game::entity::Exit, ZONE_EXIT_MAX_COUNT>, ZONE_MAX_COUNT> exitsOfZones_;
// bn::vector<bn::vector<game::entity::Shutter, ZONE_SHUTTER_MAX_COUNT>, ZONE_MAX_COUNT> shuttersOfZones_;
// bn::vector<bn::vector<bn::fixed_point, ZONE_ENTRANCE_MAX_COUNT>, ZONE_MAX_COUNT> entrancesOfZones_;

void SetCurrentZone_(int zoneIdx);
};

} // namespace sym::scene
2 changes: 1 addition & 1 deletion include/scene_Title.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Title final : public IScene
{
public:
Title();
~Title() = default;
~Title();
[[nodiscard]] bn::optional<Type> Update() final;

private:
Expand Down
29 changes: 29 additions & 0 deletions src/game_entity_Entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "game_entity_Entity.h"

namespace sym::game::entity
{

Entity::~Entity() = default;

Entity::Entity(bn::fixed_point position, bn::fixed_rect collider, const bn::sprite_item* const spriteItem)
: position_(position), collider_(collider), spriteItem_(spriteItem)
{
}

Entity::Entity(Entity&& other) noexcept
: collider_(other.collider_), sprite_(bn::move(other.sprite_)), spriteItem_(other.spriteItem_)
{
}

void Entity::FreeGraphicResource()
{
sprite_.reset();
}

void Entity::AllocateGraphicResource()
{
if (spriteItem_)
sprite_ = spriteItem_->create_sprite(collider_.position());
}

} // namespace sym::game::entity
Loading

0 comments on commit d678774

Please sign in to comment.