-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
126 additions
and
66 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,94 @@ | ||
#include "pickable.hpp" | ||
|
||
#include <functional> | ||
|
||
#include "game.hpp" | ||
#include "particle.hpp" | ||
#include "player.hpp" | ||
#include "utils.hpp" | ||
|
||
Pickable::Pickable(Sprite &&sprite, const Vector2 &position, std::function<void()> func) | ||
: sprite{ std::move(sprite) } | ||
, position{ position } | ||
Pickable Pickable::create(const Vector2 &position, const Vector2 &velocity, std::function<void()> func) | ||
{ | ||
Pickable pickable(position, func); | ||
pickable.velocity = velocity; | ||
return pickable; | ||
} | ||
|
||
Pickable Pickable::create_ore(const Vector2 &position, const Vector2 &velocity) | ||
{ | ||
return create(position, | ||
velocity, | ||
[]() | ||
{ | ||
Game::get().coins += 1; | ||
Game::get().score += 100; | ||
}); | ||
} | ||
|
||
Pickable::Pickable(const Vector2 &position, std::function<void()> func) | ||
: position{ position } | ||
, velocity{ 0.0f, 0.0f } | ||
, func{ func } | ||
{ | ||
sprite.set_frame(0); | ||
sprite.set_centered(); | ||
} | ||
|
||
bool Pickable::update() | ||
{ | ||
position.x += velocity.x; | ||
position.y += velocity.y; | ||
|
||
mask.position = position; | ||
wrap_position(position); | ||
|
||
mask.position = position; | ||
if (!Game::get().player) | ||
return true; | ||
|
||
if (Game::get().player) | ||
if (!player) | ||
{ | ||
const Player &player = *Game::get().player; | ||
const Mask player_mask = player.mask; | ||
if (mask.check_collision(Game::get().player->mask)) | ||
{ | ||
player = Game::get().player.get(); | ||
|
||
velocity = Vector2Normalize(Vector2Subtract(position, player->position)); | ||
velocity.x *= 1.1f; | ||
velocity.y *= 1.1f; | ||
|
||
if (mask.check_collision(player_mask)) | ||
sprite.scale = Vector2{ 0.86f, 0.86f }; | ||
} | ||
} | ||
else | ||
{ | ||
const float d = Vector2Distance(position, player->position); | ||
if (d < 8.0f) | ||
{ | ||
if (func) | ||
func(); | ||
|
||
return false; | ||
} | ||
const Vector2 dir = Vector2Normalize(Vector2Subtract(player->position, position)); | ||
|
||
if (d > 64.0f) | ||
{ | ||
position.x = player->position.x - dir.x * 16.0f; | ||
position.y = player->position.y - dir.y * 16.0f; | ||
} | ||
|
||
velocity.x += dir.x * 0.3f; | ||
velocity.y += dir.y * 0.3f; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void Pickable::draw() const | ||
{ | ||
if (IsTextureReady(sprite.get_texture())) | ||
{ | ||
draw_wrapped(sprite.get_destination_rect(), | ||
[&](const Vector2 &position) | ||
{ | ||
sprite.position.x = position.x; | ||
sprite.draw(); | ||
}); | ||
} | ||
|
||
if (Game::get().CONFIG.show_debug) | ||
{ | ||
draw_wrapped(Rectangle{ position.x, position.y, 10.0f, 10.0f }, | ||
[&](const Vector2 &position) | ||
{ | ||
mask.position = position; | ||
mask.draw(); | ||
}); | ||
} | ||
sprite.set_centered(); | ||
sprite.position = position; | ||
draw_wrapped(sprite.get_destination_rect(), | ||
[&](const Vector2 &position) | ||
{ | ||
sprite.position = position; | ||
sprite.draw(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include "mask.hpp" | ||
#include "sprite.hpp" | ||
#include "utils.hpp" | ||
|
||
class Player; | ||
|
||
class Pickable | ||
{ | ||
public: | ||
Pickable() {} | ||
Pickable(Sprite &&sprite, const Vector2 &position, std::function<void()> func); | ||
|
||
static Pickable create(const Vector2 &position, const Vector2 &velocity, std::function<void()> func); | ||
static Pickable create_ore(const Vector2 &position, const Vector2 &velocity); | ||
bool update(); | ||
void draw() const; | ||
|
||
mutable Sprite sprite; | ||
mutable Mask mask{ Circle{ Vector2{ 0.0f, 0.0f }, 10.0f } }; | ||
mutable Sprite sprite{ "resources/ore.aseprite" }; | ||
mutable Mask mask{ Circle{ Vector2{ 0.0f, 0.0f }, 24.0f } }; | ||
Vector2 position{ 0.0f, 0.0f }; | ||
Vector2 velocity{ 0.0f, 0.0f }; | ||
const Player *player{ nullptr }; | ||
|
||
private: | ||
Pickable() = default; | ||
Pickable(const Vector2 &position, std::function<void()> func); | ||
std::function<void()> func; | ||
|
||
DECLARE_FRIEND_OBJECT_CIRCULAR_BUFFER() | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters