Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
feat(player): limit bullets
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Aug 12, 2020
1 parent 6dc39ef commit 747d57c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions assets/scenes/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
},
{
"name": "PlayerController",
"bullet_clip": 2,
"speed": 100
},
{
Expand Down
8 changes: 7 additions & 1 deletion include/components/PlayerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ class Transform;
class PhysicsBody;

class PlayerController final : public Component {
uint8_t bulletClip_;
uint8_t speed_;
std::shared_ptr<PhysicsBody> physicsBody_;
std::weak_ptr<GameObject> bullets_;

public:
PlayerController(std::weak_ptr<GameObject> gameObject,
PlayerController(std::weak_ptr<GameObject> gameObject, uint8_t bulletClip,
uint8_t speed) noexcept;
~PlayerController() noexcept override;
void onAwake() noexcept override;
Expand All @@ -25,4 +26,9 @@ class PlayerController final : public Component {

[[nodiscard]] inline const uint8_t& speed() const noexcept { return speed_; }
inline uint8_t& speed() noexcept { return speed_; }

[[nodiscard]] inline const uint8_t& bulletClip() const noexcept {
return bulletClip_;
}
inline uint8_t& bulletClip() noexcept { return bulletClip_; }
};
10 changes: 7 additions & 3 deletions src/components/PlayerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
#include "scenes/Scene.h"

PlayerController::PlayerController(std::weak_ptr<GameObject> gameObject,
uint8_t speed) noexcept
: Component(std::move(gameObject)), speed_(speed) {}
uint8_t bulletClip, uint8_t speed) noexcept
: Component(std::move(gameObject)),
bulletClip_(bulletClip),
speed_(speed) {}

PlayerController::~PlayerController() noexcept = default;

Expand Down Expand Up @@ -56,7 +58,9 @@ void PlayerController::onUpdate() noexcept {
physicsBody_->body()->SetLinearVelocity(translation.toVec());
}

if (Input::keyDown(KeyboardKey::SPACE)) {
if (bulletClip_ && Input::keyDown(KeyboardKey::SPACE)) {
--bulletClip_;

const auto transform = gameObject().lock()->transform().lock();

const auto& sp = transform->scale().cast<float>() / 2.f;
Expand Down
1 change: 1 addition & 0 deletions src/factories/PhysicsBodyFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ PhysicsBodyFactory::~PhysicsBodyFactory() noexcept = default;
// "name": "PhysicsBody",
// "type": "static",
// "sensor": false,
// "density": 18.5,
// "restitution": 0.6,
// "data": [400, 400, 100, 100],
// "category": ["player"],
Expand Down
4 changes: 3 additions & 1 deletion src/factories/PlayerControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ PlayerControllerFactory::~PlayerControllerFactory() noexcept = default;
std::shared_ptr<PlayerController> PlayerControllerFactory::fromJson(
const Json::Value& json, std::weak_ptr<GameObject> parent) {
return std::make_shared<PlayerController>(
parent, static_cast<uint8_t>(json["speed"].asUInt()));
parent, static_cast<uint8_t>(json["bullet_clip"].asUInt()),
static_cast<uint8_t>(json["speed"].asUInt()));
}

Json::Value PlayerControllerFactory::toJson(
std::shared_ptr<PlayerController> value) const {
Json::Value json(Json::objectValue);
json["name"] = name();
json["bullet_clip"] = value->bulletClip();
json["speed"] = value->speed();
return json;
}
18 changes: 16 additions & 2 deletions src/listeners/ContactListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "listeners/ContactListener.h"

#include "components/PhysicsBody.h"
#include "components/PlayerController.h"
#include "objects/GameObject.h"
#include "utils/DebugAssert.h"

Expand All @@ -19,8 +20,21 @@ void ContactListener::BeginContact(b2Contact* contact) {
a->gameObject().lock()->name().c_str(), ac,
b->gameObject().lock()->name().c_str(), bc, collectible);

if (ac) a->destroy();
if (bc) b->destroy();
if (ac) {
a->destroy();
auto gb = b->gameObject().lock();
if (gb->name() == "Player") {
gb->getComponent<PlayerController>()->bulletClip()++;
}
}

if (bc) {
b->destroy();
auto ga = a->gameObject().lock();
if (ga->name() == "Player") {
ga->getComponent<PlayerController>()->bulletClip()++;
}
}
}

void ContactListener::EndContact(b2Contact* contact) {
Expand Down

0 comments on commit 747d57c

Please sign in to comment.