Skip to content

Commit

Permalink
Add camera edge-snapping
Browse files Browse the repository at this point in the history
- Add IndexRect to save & load map edge index info
- Add player & door
- Add camera edge-snapping
  • Loading branch information
copyrat90 committed Jun 3, 2021
1 parent 7e32b64 commit b9cc72d
Show file tree
Hide file tree
Showing 26 changed files with 959 additions and 242 deletions.
Binary file modified graphics/bg_w0_s0_0.bmp
Binary file not shown.
Binary file modified graphics/spr_door.bmp
Binary file not shown.
83 changes: 42 additions & 41 deletions graphics_source/bg_w0_s0_0.tmx

Large diffs are not rendered by default.

Binary file modified graphics_source/spr_door.aseprite
Binary file not shown.
33 changes: 33 additions & 0 deletions include/game_entity_Door.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "game_entity_IOpenableEntity.h"

#include <bn_fixed_rect.h>
#include <bn_sprite_animate_actions.h>

namespace sym::game::entity
{

class Door final : public IOpenableEntity
{
public:
Door(bn::fixed_point position, bool isOpened);

Door(Door&& other);
Door& operator=(Door&& other);

Door(const Door& other) = delete;
Door& operator=(const Door& other) = delete;

void FreeGraphicResource() final;

void Update() final;

void InitDoorOpenAction();
void InitDoorCloseAction();

private:
bn::optional<bn::sprite_animate_action<4>> action_;
};

} // namespace sym::game::entity
63 changes: 0 additions & 63 deletions include/game_entity_Entity.h

This file was deleted.

14 changes: 14 additions & 0 deletions include/game_entity_HoverButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "game_entity_IEntity.h"

namespace sym::game::entity
{

class HoverButton final : IEntity
{
public:
private:
};

} // namespace sym::game::entity
75 changes: 75 additions & 0 deletions include/game_entity_IEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

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

namespace sym::game::entity
{

class IEntity
{
public:
virtual ~IEntity() = 0;

/**
* @brief Constructor.
*
* Graphic is not allocated in here.
* You have to manually call AllocateGraphicResource().
*
* @param position center position of the Entity
* @param relativeInteractRange rect range to interact with player, relative to position.
* @param spriteItem sprite_item
*/
IEntity(bn::fixed_point position, bn::fixed_rect relativeInteractRange,
const bn::sprite_item* spriteItem = nullptr);

/**
* @brief Move constructor.
*
*/
IEntity(IEntity&& other);

/**
* @brief Move assignment operator.
*
*/
IEntity& operator=(IEntity&& other);

IEntity(const IEntity& other) = delete;
IEntity& operator=(const IEntity& other) = delete;

virtual void Update();

/**
* @brief Get the interactive range.
* Absolute coordinate.
*
*/
bn::fixed_rect GetInteractRange() const;

virtual void FreeGraphicResource();
virtual void AllocateGraphicResource(int z_order);

void SetCamera(const bn::camera_ptr& camera);

bn::fixed_point GetPosition() const;
void SetPosition(const bn::fixed_point& position);
bn::fixed GetX() const;
bn::fixed GetY() const;
void SetX(bn::fixed x);
void SetY(bn::fixed y);

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

void SyncSpritePositionToPosition_();
};

} // namespace sym::game::entity
48 changes: 48 additions & 0 deletions include/game_entity_IGravityEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include "game_entity_IEntity.h"

namespace sym::game::entity
{

class IGravityEntity : public IEntity
{
public:
virtual ~IGravityEntity() = 0;

IGravityEntity(bn::fixed_point position, bn::fixed_rect relativeInteractRange,
bn::fixed_rect relativePhysicsCollider, bool isGravityEnabledByDefault, bn::fixed gravityScale,
const bn::sprite_item* spriteItem);

IGravityEntity(IGravityEntity&& other);
IGravityEntity& operator=(IGravityEntity&& other);

IGravityEntity(const IGravityEntity& other) = delete;
IGravityEntity& operator=(const IGravityEntity& other) = delete;

/**
* @brief Get the physics collider.
* Absolute Coordinate.
*
* @return `bn::fixed_rect` collider
*/
bn::fixed_rect GetPhysicsCollider() const;

bool GetGravityEnabled() const;
void SetGravityEnabled(bool isGravityEnabled);

bn::fixed GetGravityScale() const;
void SetGravityScale(bn::fixed gravityScale);
[[maybe_unused]] bool ToggleGravityEnabled();

protected:
/**
* @brief saves collider relative to the position_.
*
*/
bn::fixed_rect relativePhysicsCollider_;
bool isGravityEnabled_;
bn::fixed gravityScale_;
};

} // namespace sym::game::entity
30 changes: 30 additions & 0 deletions include/game_entity_IOpenableEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "game_entity_IEntity.h"

namespace sym::game::entity
{

class IOpenableEntity : public IEntity
{
public:
virtual ~IOpenableEntity() = 0;

IOpenableEntity(bn::fixed_point position, bn::fixed_rect relativeInteractRange, bool isOpened,
const bn::sprite_item* spriteItem = nullptr);

IOpenableEntity(IOpenableEntity&& other);
IOpenableEntity& operator=(IOpenableEntity&& other);

IOpenableEntity(const IOpenableEntity& other) = delete;
IOpenableEntity& operator=(const IOpenableEntity& other) = delete;

bool GetOpened() const;
void SetOpened(bool);
[[maybe_unused]] bool ToggleOpened();

private:
bool isOpened_;
};

} // namespace sym::game::entity
55 changes: 55 additions & 0 deletions include/game_entity_Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include "game_entity_IGravityEntity.h"

#include <bn_any.h>
#include <bn_sprite_animate_actions.h>

namespace sym::game::entity
{

class Player final : public IGravityEntity
{
public:
Player(bn::fixed_point position);

Player(Player&& other);
Player& operator=(Player&& other);

Player(const Player& other) = delete;
Player& operator=(const Player& other) = delete;

void FreeGraphicResource() final;

void Update() final;

void InitIdleAction();
void InitJumpAction();
void InitFallAction();
void InitLandAction();
void InitMergeStartAction();
void InitMergeEndAction();

private:
static constexpr bn::fixed_rect RELATIVE_INTERACT_RANGE = {{0, 0}, {32, 32}};
static constexpr bn::fixed_rect RELATIVE_PHYSICS_COLLIDER = {{0, 0}, {26, 26}};
static constexpr bool IS_GRAVITY_ENABLED_BY_DEFAULT = true;
static constexpr bn::fixed GRAVITY_SCALE = 1;

static constexpr int IDLE_ACTION_WAIT_UPDATE = 30;
static constexpr int OTHER_ACTIONS_WAIT_UPDATE = 10;

bn::optional<bn::sprite_animate_action<2>> action2_;
bn::optional<bn::sprite_animate_action<3>> action3_;
/**
* @brief Additional wait update before the InitIdleAction() is called.
* If it is `-1`, InitIdleAction() is not called.
* (i.e. the last animation frame is used after the action ends.)
*
*/
int additionalWaitUpdateCount = -1;

void DestroyActions_();
};

} // namespace sym::game::entity
14 changes: 8 additions & 6 deletions include/game_entity_Symbol.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "game_entity_Entity.h"
#include "game_entity_IGravityEntity.h"

namespace sym::game::entity
{

class Symbol final : Entity
class Symbol final : public IGravityEntity
{
public:
static constexpr int COMPLEX_SYMBOL_START_NUM = 100;
Expand All @@ -25,19 +25,21 @@ class Symbol final : Entity
};

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

Symbol(const Symbol& other) = delete;
Symbol& operator=(const Symbol& other) = delete;

void AllocateGraphicResource() final;
void AllocateGraphicResource(int z_order) final;

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

private:
static constexpr bool IS_APPLY_GRAVITY = true;
static constexpr bn::fixed_rect RELATIVE_INTERACT_RANGE = {{0, 0}, {24, 16}};
static constexpr bool IS_GRAVITY_ENABLED_BY_DEFAULT = true;
static constexpr bn::fixed GRAVITY_SCALE = 1;

Symbol::Type type_;
};
Expand Down
10 changes: 10 additions & 0 deletions include/helper_rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ namespace sym::helper::rect
return MakeFixedRectByTopLeftAndSize(topLeft, size);
}

[[nodiscard]] constexpr bn::fixed_rect operator+(const bn::fixed_rect& rect, const bn::fixed_point& point)
{
return {rect.position() + point, rect.dimensions()};
}

[[nodiscard]] constexpr bn::fixed_rect operator+(const bn::fixed_point& point, const bn::fixed_rect& rect)
{
return operator+(rect, point);
}

} // namespace sym::helper::rect
Loading

0 comments on commit b9cc72d

Please sign in to comment.