Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scared Gold Bomb #2559

Merged
merged 10 commits into from
Sep 2, 2023
Binary file added data/images/creatures/gold_bomb/flee-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/flee-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/flee-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/flee-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/flee-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/flee-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/flee-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/flee-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 71 additions & 2 deletions data/images/creatures/gold_bomb/gold_bomb.sprite
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,83 @@
(hitbox 14 19 32 32)
(mirror-action "left"))

(action
(name "flee-left")
(fps 25.0)
(hitbox 14 19 32 32)
(images "flee-0.png"
"flee-1.png"
"flee-2.png"
"flee-3.png"
"flee-4.png"
"flee-5.png"
"flee-6.png"
"flee-7.png"))

(action
(name "flee-right")
(fps 25.0)
(hitbox 14 19 32 32)
(mirror-action "flee-left"))

(action
(name "scared-left")
(fps 25.0)
(hitbox 14 19 32 32)
(images "scared-0.png"
"scared-1.png"))

(action
(name "scared-right")
(fps 25.0)
(hitbox 14 19 32 32)
(mirror-action "scared-left"))

(action
(name "recover-left")
(fps 20.0)
(loops 1)
(hitbox 14 19 32 32)
(images "scared-2.png"
"scared-3.png"
"scared-4.png"
"scared-5.png"
"scared-6.png"
"scared-7.png"
"scared-4.png"
"scared-5.png"
"scared-6.png"
"scared-7.png"
"scared-8.png"
"scared-9.png"
"scared-9.png"
"scared-9.png"
"scared-9.png"
"scared-9.png"
"scared-10.png"
"scared-11.png"
"scared-11.png"
"scared-11.png"
"scared-11.png"
"scared-11.png"
"scared-10.png"
"scared-7.png"))

(action
(name "recover-right")
(fps 20.0)
(loops 1)
(hitbox 14 19 32 32)
(mirror-action "recover-left"))

(action
(name "iced-left")
(hitbox 5 8 32 32)
(hitbox 14 19 32 32)
(images "left-0.png"))

(action
(name "iced-right")
(hitbox 5 8 32 32)
(hitbox 14 19 32 32)
(mirror-action "iced-left"))

(action
Expand Down
Binary file added data/images/creatures/gold_bomb/scared-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/images/creatures/gold_bomb/scared-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 158 additions & 6 deletions src/badguy/goldbomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "audio/sound_manager.hpp"
#include "audio/sound_source.hpp"
#include "badguy/owl.hpp"
#include "badguy/haywire.hpp"
#include "badguy/bomb.hpp"
Vankata453 marked this conversation as resolved.
Show resolved Hide resolved
#include "object/coin_explode.hpp"
#include "object/explosion.hpp"
#include "object/player.hpp"
Expand All @@ -29,14 +31,29 @@
#include "supertux/sector.hpp"
#include "util/reader_mapping.hpp"

const float HOP_HEIGHT = -250.f;
const float REALIZE_TIME = 0.5f;

// SAFE_DIST >= REALIZE_DIST
const float REALIZE_DIST = 32.f * 8.f;
const float SAFE_DIST = 32.f * 10.f;

const float NORMAL_WALK_SPEED = 80.0f;
const float FLEEING_WALK_SPEED = 180.0f;
const int NORMAL_MAX_DROP_HEIGHT = 16;
const int FLEEING_MAX_DROP_HEIGHT = 600;
Vankata453 marked this conversation as resolved.
Show resolved Hide resolved

GoldBomb::GoldBomb(const ReaderMapping& reader) :
WalkingBadguy(reader, "images/creatures/gold_bomb/gold_bomb.sprite", "left", "right"),
tstate(STATE_NORMAL),
m_realize_timer(),
ticking(),
m_exploding_sprite(SpriteManager::current()->create("images/creatures/mr_bomb/ticking_glow/ticking_glow.sprite"))
{
walk_speed = 80;
max_drop_height = 16;
assert(SAFE_DIST >= REALIZE_DIST);

walk_speed = NORMAL_WALK_SPEED;
max_drop_height = NORMAL_MAX_DROP_HEIGHT;

//Prevent stutter when Tux jumps on Gold Bomb
SoundManager::current()->preload("sounds/explosion.wav");
Expand All @@ -57,7 +74,11 @@ GoldBomb::collision_solid(const CollisionHit& hit)
m_physic.set_velocity_y(0);
update_on_ground_flag(hit);
return;
} else if (tstate != STATE_NORMAL && (hit.left || hit.right)) {
cornered();
return;
}

WalkingBadguy::collision_solid(hit);
}

Expand Down Expand Up @@ -90,7 +111,7 @@ GoldBomb::collision_player(Player& player, const CollisionHit& hit)
HitResponse
GoldBomb::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
{
if (tstate == STATE_TICKING)
if (tstate != STATE_NORMAL)
return FORCE_MOVE;
return WalkingBadguy::collision_badguy(badguy, hit);
}
Expand All @@ -107,7 +128,7 @@ GoldBomb::collision_squished(GameObject& object)
kill_fall();
return true;
}
if (is_valid() && tstate == STATE_NORMAL) {
if (is_valid() && tstate != STATE_TICKING) {
tstate = STATE_TICKING;
m_frozen = false;
set_action(m_dir == Direction::LEFT ? "ticking-left" : "ticking-right", 1);
Expand Down Expand Up @@ -141,15 +162,113 @@ GoldBomb::active_update(float dt_sec)
}
return;
}
if (is_grabbed())

if (m_frozen) return;

if ((tstate == STATE_FLEEING || tstate == STATE_CORNERED) && on_ground() && might_fall(FLEEING_MAX_DROP_HEIGHT+1))
{
// also check for STATE_CORNERED just so
// the bomb doesnt automatically turn around
cornered();
return;
}
WalkingBadguy::active_update(dt_sec);

MovingObject* obj = nullptr;
std::vector<MovingObject*> objs = Sector::get().get_nearby_objects(get_bbox().get_middle(), SAFE_DIST);
for (size_t i = 0; i < objs.size(); i++)
Vankata453 marked this conversation as resolved.
Show resolved Hide resolved
{
obj = objs[i];

auto player = dynamic_cast<Player*>(obj);
if (player && !player->get_ghost_mode()) break;

auto haywire = dynamic_cast<Haywire*>(obj);
if (haywire && haywire->is_exploding()) break;

auto bomb = dynamic_cast<Bomb*>(obj);
if (bomb) break;

auto goldbomb = dynamic_cast<GoldBomb*>(obj);
if (goldbomb && goldbomb->is_ticking()) break;

obj = nullptr;
}

if (!obj)
{
if (tstate == STATE_CORNERED)
{
set_action("recover", m_dir);
if (!m_sprite->animation_done()) return;
}
tstate = STATE_NORMAL;
m_physic.set_velocity_x(NORMAL_WALK_SPEED * (m_dir == Direction::LEFT ? -1 : 1));
m_physic.set_acceleration_x(0);
set_action(m_dir);
max_drop_height = NORMAL_MAX_DROP_HEIGHT;
set_walk_speed(NORMAL_WALK_SPEED);
return;
}

const Vector p1 = get_bbox().get_middle();
const Vector p2 = obj->get_bbox().get_middle();
const Vector vecdist = p2-p1;

if (glm::length(vecdist) > REALIZE_DIST && tstate == STATE_NORMAL) return;

switch (tstate)
{

case STATE_FLEEING:
if (m_dir == (vecdist.x > 0 ? Direction::LEFT : Direction::RIGHT)) return;
[[fallthrough]];

case STATE_NORMAL:
{
if (!on_ground()) break;

// Gold bomb is solid therefore raycast from
// one of the upper corners of the hitbox.
// (grown 1 just to make sure it doesnt interfere.)
const Rectf eye = get_bbox().grown(1.f);
if (!Sector::get().free_line_of_sight(
vecdist.x <= 0 ? eye.p1() : Vector(eye.get_right(), eye.get_top()),
obj->get_bbox().get_middle(),
false,
obj
)) break;

set_walk_speed(0);
m_physic.set_velocity_y(HOP_HEIGHT);
m_physic.set_velocity_x(0);
m_physic.set_acceleration_x(0);
m_dir = vecdist.x > 0 ? Direction::RIGHT : Direction::LEFT;
m_sprite->set_action("flee", m_dir);
tstate = STATE_REALIZING;
m_realize_timer.start(REALIZE_TIME);
break;
}

case STATE_REALIZING:
if (!m_realize_timer.check()) break;

flee(vecdist.x > 0 ? Direction::LEFT : Direction::RIGHT);
break;

default: break;

Vankata453 marked this conversation as resolved.
Show resolved Hide resolved
}
}

void
GoldBomb::draw(DrawingContext& context)
{
m_sprite->draw(context.color(), get_pos(), m_layer, m_flip);
//const Rectf realizerect(get_bbox().get_middle()-Vector(REALIZE_DIST, REALIZE_DIST), get_bbox().get_middle()+Vector(REALIZE_DIST, REALIZE_DIST));
//const Rectf saferect(get_bbox().get_middle()-Vector(SAFE_DIST, SAFE_DIST), get_bbox().get_middle()+Vector(SAFE_DIST, SAFE_DIST));
//context.color().draw_filled_rect(realizerect, Color::from_rgba8888(255, 0, 0, 100), realizerect.get_size().width/2, 100);
//context.color().draw_filled_rect(saferect, Color::from_rgba8888(0, 255, 0, 100), saferect.get_size().width/2, 100);
Vankata453 marked this conversation as resolved.
Show resolved Hide resolved
if (tstate == STATE_TICKING)
{
m_exploding_sprite->set_blend(Blend::ADD);
Expand Down Expand Up @@ -261,7 +380,7 @@ GoldBomb::ungrab(MovingObject& object, Direction dir_)
void
GoldBomb::freeze()
{
if (tstate == STATE_NORMAL) {
if (tstate != STATE_TICKING) {
WalkingBadguy::freeze();
}
}
Expand Down Expand Up @@ -292,4 +411,37 @@ void GoldBomb::play_looping_sounds()
}
}

void
GoldBomb::flee(Direction dir)
{
set_walk_speed(FLEEING_WALK_SPEED);
max_drop_height = FLEEING_MAX_DROP_HEIGHT;
m_dir = dir;

const float speed = FLEEING_WALK_SPEED * (m_dir == Direction::LEFT ? -1 : 1);
m_physic.set_acceleration_x(speed);
m_physic.set_velocity_x(speed);

if (get_action() == dir_to_string(m_dir))
m_sprite->set_animation_loops(-1);
else
set_action("flee", m_dir);

tstate = STATE_FLEEING;
}

void
GoldBomb::cornered()
{
if (tstate == STATE_CORNERED) return;

set_walk_speed(0);
m_physic.set_velocity_x(0);
m_physic.set_acceleration_x(0);

set_action("scared", m_dir);

tstate = STATE_CORNERED;
}

/* EOF */
12 changes: 10 additions & 2 deletions src/badguy/goldbomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,25 @@ class GoldBomb final : public WalkingBadguy
virtual void stop_looping_sounds() override;
virtual void play_looping_sounds() override;

void flee(Direction dir);
void cornered();

inline bool is_ticking() const { return tstate == STATE_TICKING; }

protected:
virtual bool collision_squished(GameObject& object) override;

private:
enum Ticking_State {
STATE_NORMAL,
STATE_TICKING
STATE_TICKING,
STATE_REALIZING,
STATE_FLEEING,
STATE_CORNERED
};

private:
Ticking_State tstate;
Timer m_realize_timer;

std::unique_ptr<SoundSource> ticking;
SpritePtr m_exploding_sprite;
Expand Down
Loading
Loading