Skip to content

Commit

Permalink
Add HoverButton interaction
Browse files Browse the repository at this point in the history
- Toggle the HoverButton with left & right hand
- Open/Close the appropriate doors & shutters
- Add a sound effect for toggling buttons
  • Loading branch information
copyrat90 committed Jun 9, 2021
1 parent 4f92a97 commit c769b91
Show file tree
Hide file tree
Showing 37 changed files with 725 additions and 252 deletions.
Binary file added audio/sfx_toggle_button.wav
Binary file not shown.
Binary file modified graphics/bg_w0_s0_0.bmp
Binary file not shown.
Binary file modified graphics/spr_hover_button.bmp
Binary file not shown.
79 changes: 44 additions & 35 deletions graphics_source/bg_w0_s0_0.tmx

Large diffs are not rendered by default.

Binary file modified graphics_source/spr_hover_button.aseprite
Binary file not shown.
13 changes: 10 additions & 3 deletions include/game_entity_Door.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Door final : public IOpenableEntity
*
* @param textNumber number which is shown above the door.
*/
Door(bn::fixed_point position, bool isOpened, int textNumber);
Door(bn::fixed_point position, bool isOpenedByDefault, int textNumber);

Door(Door&& other) = delete;
Door& operator=(Door&& other) = delete;
Expand All @@ -29,11 +29,18 @@ class Door final : public IOpenableEntity

void Update() final;

void InitDoorOpenAction();
void InitDoorCloseAction();
/**
* @brief Toggle opened (also animates door)
*
* @return `true` if the door is opened
*/
[[maybe_unused]] bool ToggleOpened() final;

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

void InitDoorOpenAction_();
void InitDoorCloseAction_();
};

} // namespace sym::game::entity
14 changes: 12 additions & 2 deletions include/game_entity_HoverButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ class HoverButton final : public IButtonEntity

void Update() final;

void InitButtonOnAction();
void InitButtonOffAction();
[[nodiscard]] bool CanButtonBeToggled() final;
/**
* @brief Toggles button (and also animates it)
*
* `CanButtonBeToggled()` needs to be checked first before using this function.
*
* @return `true` if button is on
*/
[[maybe_unused]] bool ToggleButtonOn() final;

private:
bn::optional<bn::sprite_animate_action<2>> action_;

void InitButtonOnAction_();
void InitButtonOffAction_();
};

} // namespace sym::game::entity
12 changes: 10 additions & 2 deletions include/game_entity_IButtonEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ class IButtonEntity : public INumberTextEntity
IButtonEntity(const IButtonEntity& other) = delete;
IButtonEntity& operator=(const IButtonEntity& other) = delete;

bool GetButtonOn() const;
void SetButtonOn(bool isButtonOn);
[[nodiscard]] bool GetButtonOn() const;
// void SetButtonOn(bool isButtonOn);
[[nodiscard]] virtual bool CanButtonBeToggled() = 0;
/**
* @brief `CanButtonBeToggled()` needs to be checked first before using this function.
* `IButtonEntity::ToggleButtonOn()` must be called somewhere in the Button implementation.
*
* @return `true` if button is on
*/
[[maybe_unused]] virtual bool ToggleButtonOn() = 0;

private:
bool isButtonOn_;
Expand Down
2 changes: 1 addition & 1 deletion include/game_entity_INumberTextEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class INumberTextEntity : public IEntity

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

private:
protected:
bn::optional<bn::sprite_ptr> numberSprite_;
int textNumber_;
bn::fixed_point relativeNumberTextPosition_;
Expand Down
23 changes: 16 additions & 7 deletions include/game_entity_IOpenableEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@ class IOpenableEntity : public INumberTextEntity
virtual ~IOpenableEntity() = 0;

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

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

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

bool GetOpened() const;
void SetOpened(bool);
[[maybe_unused]] bool ToggleOpened();
void AllocateGraphicResource(int z_order) override;

private:
[[nodiscard]] bool GetOpened() const;
// void SetOpened(bool);

/**
* @brief `IOpenableEntity::ToggleOpened()` must be called somewhere in the Openable implementation.
*
* @return `true` if it is opened
*/
[[maybe_unused]] virtual bool ToggleOpened() = 0;

protected:
const bool isOpenedByDefault_;
bool isOpened_;
};

Expand Down
23 changes: 14 additions & 9 deletions include/game_entity_Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "game_entity_IGravityEntity.h"

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

namespace sym::game::entity
Expand Down Expand Up @@ -30,15 +29,15 @@ class Player final : public IGravityEntity
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;
[[nodiscard]] bn::fixed_rect GetLeftSymbolPickupRange() const;
[[nodiscard]] bn::fixed_rect GetRightSymbolPickupRange() const;
[[nodiscard]] bn::fixed_rect GetLeftButtonInteractRange() const;
[[nodiscard]] bn::fixed_rect GetRightButtonInteractRange() const;
[[nodiscard]] bn::fixed_point GetLeftSymbolPosition() const;
[[nodiscard]] bn::fixed_point GetRightSymbolPosition() const;
[[nodiscard]] bn::fixed_point GetMergeSymbolPosition() const;

private:
bn::optional<bn::sprite_animate_action<2>> action2_;
bn::optional<bn::sprite_animate_action<3>> action3_;
/**
Expand All @@ -49,6 +48,12 @@ class Player final : public IGravityEntity
*/
int additionalWaitUpdateCount = -1;

/**
* @brief Updates action.
*
* @return `true` if action is done, otherwise `false`
*/
bool UpdateAction_();
void DestroyActions_();
};

Expand Down
14 changes: 12 additions & 2 deletions include/game_entity_PressureButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ class PressureButton final : public IButtonEntity

void Update() final;

void InitButtonOnAction();
void InitButtonOffAction();
[[nodiscard]] bool CanButtonBeToggled() final;
/**
* @brief Toggles button (and also animates it)
*
* `CanButtonBeToggled()` needs to be checked first before using this function.
*
* @return `true` if button is on
*/
[[maybe_unused]] bool ToggleButtonOn() final;

private:
bn::optional<bn::sprite_animate_action<2>> action_;

void InitButtonOnAction_();
void InitButtonOffAction_();
};

} // namespace sym::game::entity
13 changes: 10 additions & 3 deletions include/game_entity_Shutter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Shutter final : public IOpenableEntity
*
* @param textNumber number which is shown above the Shutter.
*/
Shutter(bn::fixed_point position, bool isOpened, int textNumber);
Shutter(bn::fixed_point position, bool isOpenedByDefault, int textNumber);

Shutter(Shutter&& other) = delete;
Shutter& operator=(Shutter&& other) = delete;
Expand All @@ -29,11 +29,18 @@ class Shutter final : public IOpenableEntity

void Update() final;

void InitShutterOpenAction();
void InitShutterCloseAction();
/**
* @brief Toggle opened (also animates shutter)
*
* @return `true` if the shutter is opened
*/
[[maybe_unused]] bool ToggleOpened() final;

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

void InitShutterOpenAction_();
void InitShutterCloseAction_();
};

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

#include "game_system_ISystem.h"

namespace sym::game::system
{

class ButtonInteraction final : public ISystem
{
public:
ButtonInteraction(scene::GameState& state);

void Update() final;

private:
static constexpr int KEYPRESS_LASTING_UPDATE_COUNT = 5;
int lKeyLastingCount = -1;
int rKeyLastingCount = -1;

void HoverButtonPlayerInteract_();
void HoverButtonThrownSymbolInteract_();

void UpdateKeyLastingCount_();
bool IsLKeyPressLasts_() const;
bool IsRKeyPressLasts_() const;
void ResetLKeyPress_();
void ResetRKeyPress_();
void ToggleOpenedHoverButtonAssociatedOpenables_(int hoverButtonIdx);
};

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

#include "scene_GameState.h"

namespace sym::game::system
{

class ISystem
{
public:
virtual ~ISystem() = default;
ISystem(scene::GameState& state) : state_(state)
{
}

virtual void Update() = 0;

protected:
scene::GameState& state_;
};

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

#include "game_system_ISystem.h"

namespace sym::game::system
{

class PlayerMovement final : public ISystem
{
public:
PlayerMovement(scene::GameState& state);

void Update() final;

private:
};

} // namespace sym::game::system
63 changes: 13 additions & 50 deletions include/scene_Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,18 @@

#include "scene_IScene.h"

#include <bn_affine_bg_ptr.h>
#include <bn_array.h>
#include <bn_camera_ptr.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_Door.h"
#include "game_entity_HoverButton.h"
#include "game_entity_Player.h"
#include "game_entity_PressureButton.h"
#include "game_entity_Shutter.h"
#include "game_entity_Symbol.h"
#include "game_stage_Id.h"
#include "game_stage_StageInfo.h"
#include "game_system_ButtonInteraction.h"
#include "game_system_PlayerMovement.h"
#include "scene_GameState.h"

namespace sym::scene
{
using namespace game;

class Game final : public IScene
{
public:
Game(game::Status& status);
Game(scene::Param& sceneParam);
~Game();

[[nodiscard]] bn::optional<Type> Update() final;
Expand All @@ -38,36 +22,15 @@ class Game final : public IScene
static constexpr int FADE_IN_UPDATE_COUNT = 30;
static constexpr int FADE_OUT_UPDATE_COUNT = 30;

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_SHUTTER_MAX_COUNT = 4;

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

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

int currentZoneIdx_;
bn::affine_bg_ptr currentMapBg_;

bn::camera_ptr camera_;
bn::fixed_rect zoneBoundary_;

// Movable entities.
game::entity::Player player_;
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_;
/**
* @brief All member variables reside in here.
* to easily pass states by reference into Systems.
*
*/
GameState state_;

// Fixed entities.
bn::vector<bn::vector<game::entity::Door, ZONE_DOOR_MAX_COUNT>, ZONE_MAX_COUNT> doorsOfZones_;
bn::vector<bn::vector<game::entity::Shutter, ZONE_SHUTTER_MAX_COUNT>, ZONE_MAX_COUNT> shuttersOfZones_;
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_;
system::PlayerMovement playerMovement_;
system::ButtonInteraction buttonInteraction_;

void SetCurrentZone_(int zoneIdx);
};
Expand Down
Loading

0 comments on commit c769b91

Please sign in to comment.