Skip to content

Commit

Permalink
Scriptable granito wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusGuy committed Apr 25, 2024
1 parent f915887 commit 9a2fb2d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 23 deletions.
115 changes: 96 additions & 19 deletions src/badguy/granito.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ Granito::active_update(float dt_sec)
m_has_waved = true;
}
}
else
else if (m_type != SCRIPTABLE)
{
try_wave();
}
}

if (m_type == DEFAULT && try_jump())
if (m_type == SCRIPTABLE || (m_type == DEFAULT && try_jump()))
{
WalkingBadguy::active_update(dt_sec);
return;
Expand Down Expand Up @@ -152,20 +152,12 @@ Granito::active_update(float dt_sec)
// Walk/stop
if (walk_speed > 0)
{
walk_speed = 0;
m_state = STATE_STAND;
m_original_state = STATE_STAND;
m_physic.set_velocity_x(0);
set_action("stand", m_dir);
stand();
}
else
{
m_dir = (gameRandom.rand(2) == 0 ? Direction::LEFT : Direction::RIGHT);
walk_speed = 80;
m_state = STATE_WALK;
m_original_state = STATE_WALK;
m_physic.set_velocity_x(80 * (m_dir == Direction::LEFT ? -1 : 1));
set_action(m_dir);
walk();
}
}

Expand All @@ -185,7 +177,7 @@ Granito::active_update(float dt_sec)
HitResponse
Granito::collision_player(Player& player, const CollisionHit& hit)
{
if (m_state == STATE_SIT || m_type == WALK) return FORCE_MOVE;
if (m_state == STATE_SIT || m_type == WALK || m_type == SCRIPTABLE) return FORCE_MOVE;

if (hit.top)
{
Expand Down Expand Up @@ -263,6 +255,7 @@ Granito::get_types() const
{ "default", _("Default") },
{ "standing", _("Standing") },
{ "walking", _("Walking") },
{ "scriptable", _("Scriptable") },

// Small granito only
{ "sitting", _("Sitting") }
Expand All @@ -282,6 +275,7 @@ Granito::after_editor_set()
case SIT:
set_action("sit", m_dir);
break;
case SCRIPTABLE:
case STAND:
set_action("stand", m_dir);
break;
Expand Down Expand Up @@ -314,6 +308,7 @@ Granito::initialize()
set_action("sit", m_dir);
break;

case SCRIPTABLE:
case STAND:
set_action("stand", m_dir);
break;
Expand Down Expand Up @@ -371,6 +366,77 @@ Granito::wave()
set_action("wave", m_dir, 1);
}

void Granito::sit()
{
walk_speed = 0;
m_state = STATE_SIT;
m_original_state = STATE_SIT;
m_physic.set_velocity_x(0);

if (!m_airborne)
{
float oldheight = get_bbox().get_size().height;
set_action("sit", m_dir);

float height = get_bbox().get_size().height;
set_pos(Vector(get_bbox().get_left(), get_bbox().get_top() + oldheight - height));
}
else
{
set_action("sit", m_dir);
}
}

void Granito::turn(const std::string& direction)
{
m_dir = string_to_dir(direction);
switch (m_state)
{
case STATE_WALK:
walk();
break;

case STATE_STAND:
set_action("stand", m_dir);
break;

case STATE_SIT:
set_action("sit", m_dir);
break;

case STATE_LOOKUP:
set_action("lookup", m_dir);
break;

case STATE_JUMPING:
set_action("jump", m_dir);
break;

default:
break;
}
}

void
Granito::walk()
{
walk_speed = 80;
m_state = STATE_WALK;
m_original_state = STATE_WALK;
m_physic.set_velocity_x(80 * (m_dir == Direction::LEFT ? -1 : 1));
set_action(m_dir);
}

void
Granito::stand()
{
walk_speed = 0;
m_state = STATE_STAND;
m_original_state = STATE_STAND;
m_physic.set_velocity_x(0);
set_action("stand", m_dir);
}

bool
Granito::try_jump()
{
Expand Down Expand Up @@ -427,16 +493,27 @@ Granito::restore_original_state()

if (m_state == STATE_WALK)
{
set_action(m_dir);
walk_speed = 80;
m_physic.set_velocity_x(80 * (m_dir == Direction::LEFT ? -1 : 1));
walk();
}
else
{
set_action("stand", m_dir);
walk_speed = 0;
m_physic.set_velocity_x(0);
stand();
}
}

void
Granito::register_class(ssq::VM& vm)
{
ssq::Class cls = vm.addAbstractClass<Granito>("Granito", vm.findClass("BadGuy"));

cls.addFunc("wave", &Granito::wave);
cls.addFunc("sit", &Granito::sit);
cls.addFunc("turn", &Granito::turn);
cls.addFunc("set_walking", &Granito::set_walking);
cls.addFunc("walk", &Granito::walk);
cls.addFunc("stand", &Granito::stand);
cls.addFunc("jump", &Granito::jump);
cls.addFunc("get_state", &Granito::get_state);
}

/* EOF */
86 changes: 82 additions & 4 deletions src/badguy/granito.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ class GranitoBig;
/** Interactable friendly NPC */
class Granito : public WalkingBadguy
{
public:
static void register_class(ssq::VM& vm);

public:
Granito(const ReaderMapping& reader,
const std::string& sprite_name = "images/creatures/granito/granito.sprite",
int layer = LAYER_OBJECTS);

/** \addtogroup GameObject
@{ */
virtual void active_update(float dt_sec) override;

virtual HitResponse collision_player(Player& player, const CollisionHit& hit) override;
virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;

static std::string class_name() { return "granito"; }
virtual std::string get_class_name() const override { return class_name(); }
virtual std::string get_exposed_class_name() const override { return "Granito"; }
static std::string display_name() { return _("Granito"); }
virtual std::string get_display_name() const override { return display_name(); }

Expand All @@ -47,6 +53,76 @@ class Granito : public WalkingBadguy

virtual GameObjectTypes get_types() const override;
virtual void after_editor_set() override;
/** @} */

// FIXME: Is this a good name?
/** \addtogroup GranitoAPI
@{ */

/**
* @scripting
* @description Makes the Granito wave.
*/
void wave();

/**
* @scripting
* @description Makes the Granito sit.
*/
void sit();

/**
* @scripting
* @description Makes the Granito sit.
* @param string $direction Direction to turn to. Can be "left or "right".
*/
void turn(const std::string& direction);

/**
* @scripting
* @description Sets the walking state for the Granito.
* @param bool $walking
*/
void set_walking(bool walking)
{
if (walking)
walk();
else
stand();
}

/**
* @scripting
* @description Makes the Granito walk.
*/
void walk();

/**
* @scripting
* @description Makes the Granito stand, or stop if walking.
*/
void stand();

/**
* @scripting
* @description Makes the Granito jump.
*/
void jump();

//TODO: No way to expose enums?
/**
* @scripting
* @description Gets the current granito state.
* 0 - ""SIT""
* 1 - ""STAND""
* 2 - ""WALK""
* 3 - ""WAVE""
* 4 - ""LOOKUP""
* 5 - ""JUMPING""
*/
int get_state() { return static_cast<int>(m_state); }

/** @} */

protected:
virtual void initialize() override;
Expand All @@ -55,7 +131,12 @@ class Granito : public WalkingBadguy
void activate() override;

protected:
enum Type { DEFAULT, STAND, WALK, SIT };
enum Type { DEFAULT, STAND, WALK, SCRIPTABLE, SIT };

/**
* NOTE: When changing this, make sure to also change
* the description for get_state()
*/
enum State
{
STATE_SIT,
Expand All @@ -68,10 +149,7 @@ class Granito : public WalkingBadguy

protected:
virtual bool try_wave();
void wave();

virtual bool try_jump();
void jump();

void restore_original_state();

Expand Down
1 change: 1 addition & 0 deletions src/supertux/game_object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ GameObjectFactory::register_objects(ssq::VM& vm)
Torch::register_class(vm);
WillOWisp::register_class(vm);
Wind::register_class(vm);
Granito::register_class(vm);
}

std::unique_ptr<GameObject>
Expand Down

0 comments on commit 9a2fb2d

Please sign in to comment.