Skip to content

Commit

Permalink
Switch Player from MovingObject to MovingSprite
Browse files Browse the repository at this point in the history
This exposes some Squirrel methods such as set_sprite. Pretty fun.

Big tux is still jank but this may be somewhere in the file.
  • Loading branch information
Swagtoy committed Nov 29, 2024
1 parent 8c48467 commit 671da81
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
68 changes: 35 additions & 33 deletions src/object/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "audio/sound_manager.hpp"
#include "badguy/badguy.hpp"
#include "collision/collision_group.hpp"
#include "control/codecontroller.hpp"
#include "control/input_manager.hpp"
#include "editor/editor.hpp"
Expand Down Expand Up @@ -157,6 +158,7 @@ const int MAX_ICE_BULLETS = 2;
} // namespace

Player::Player(PlayerStatus& player_status, const std::string& name_, int player_id) :
MovingSprite({0, 0}, "images/creatures/tux/tux.sprite", 9001, COLGROUP_MOVING),
m_id(player_id),
m_target(nullptr),
m_deactivated(false),
Expand Down Expand Up @@ -225,7 +227,6 @@ Player::Player(PlayerStatus& player_status, const std::string& name_, int player
// if/when we have complete penny gfx, we can
// load those instead of Tux's sprite in the
// constructor
m_sprite(SpriteManager::current()->create("images/creatures/tux/tux.sprite")),
m_swimming_angle(0),
m_swimming_accel_modifier(100.f),
m_water_jump(false),
Expand Down Expand Up @@ -263,6 +264,7 @@ Player::Player(PlayerStatus& player_status, const std::string& name_, int player

m_col.set_size(TUX_WIDTH, is_big() ? BIG_TUX_HEIGHT : SMALL_TUX_HEIGHT);


m_sprite->set_angle(0.0f);
//m_santahatsprite->set_angle(0.0f);

Expand Down Expand Up @@ -1955,13 +1957,13 @@ Player::set_bonus(BonusType type, bool animate)
if (animate) {
m_growing = true;
if (m_climbing)
m_sprite->set_action("climbgrow", m_dir, 1);
set_action("climbgrow", m_dir, 1);
else if (m_swimming)
m_sprite->set_action("swimgrow", m_dir, 1);
set_action("swimgrow", m_dir, 1);
else if (m_sliding)
m_sprite->set_action("slidegrow", m_dir, 1);
set_action("slidegrow", m_dir, 1);
else
m_sprite->set_action("grow", m_dir , 1);
set_action("grow", m_dir , 1);
}
}

Expand Down Expand Up @@ -2065,7 +2067,7 @@ Player::draw(DrawingContext& context)
/* Set Tux sprite action */
if (m_dying) {
m_sprite->set_angle(0.0f);
m_sprite->set_action("gameover");
set_action("gameover");
}
else if (m_growing)
{
Expand All @@ -2082,28 +2084,28 @@ Player::draw(DrawingContext& context)
else if (m_climbing) {
action = "climbgrow";
}
m_sprite->set_action(action + sa_postfix, Sprite::LOOPS_CONTINUED);
set_action(action + sa_postfix, Sprite::LOOPS_CONTINUED);
}
else if (m_stone) {
m_sprite->set_action("earth-stone");
set_action("earth-stone");
}
else if (m_climbing) {
m_sprite->set_action(sa_prefix+"-climb"+sa_postfix);
set_action(sa_prefix+"-climb"+sa_postfix);

// Avoid flickering briefly after growing on ladder
if ((m_physic.get_velocity_x()==0)&&(m_physic.get_velocity_y()==0))
m_sprite->pause_animation();
}
else if (m_backflipping) {
m_sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
set_action(sa_prefix+"-backflip"+sa_postfix);
}
else if (m_sliding) {
if (m_jumping || m_is_slidejump_falling) {
m_sprite->set_action(sa_prefix +"-slidejump"+ sa_postfix);
set_action(sa_prefix +"-slidejump"+ sa_postfix);
}
else {
const bool was_growing_before = (m_sprite->get_action().substr(0, 9) == "slidegrow");
m_sprite->set_action(sa_prefix + "-slide" + sa_postfix);
set_action(sa_prefix + "-slide" + sa_postfix);
if (m_was_crawling_before_slide || was_growing_before)
{
m_sprite->set_frame(m_sprite->get_frames()); // Skip the "duck" animation when coming from crawling or slidegrowing
Expand All @@ -2112,13 +2114,13 @@ Player::draw(DrawingContext& context)
}
}
else if (m_duck && is_big() && !m_swimming && !m_crawl && !m_stone) {
m_sprite->set_action(sa_prefix+"-duck"+sa_postfix);
set_action(sa_prefix+"-duck"+sa_postfix);
}
else if (m_crawl)
{
if (on_ground())
{
m_sprite->set_action(sa_prefix + "-crawl" + sa_postfix);
set_action(sa_prefix + "-crawl" + sa_postfix);
if (m_physic.get_velocity_x() != 0.f) {
m_sprite->resume_animation();
}
Expand All @@ -2127,26 +2129,26 @@ Player::draw(DrawingContext& context)
}
}
else {
m_sprite->set_action(sa_prefix + "-slidejump" + sa_postfix);
set_action(sa_prefix + "-slidejump" + sa_postfix);
}
}
else if (m_skidding_timer.started() && !m_skidding_timer.check() && !m_swimming) {
m_sprite->set_action(sa_prefix + "-skid" + sa_postfix);
set_action(sa_prefix + "-skid" + sa_postfix);
}
else if (m_kick_timer.started() && !m_kick_timer.check() && !m_swimming && !m_water_jump) {
m_sprite->set_action(sa_prefix+"-kick"+sa_postfix);
set_action(sa_prefix+"-kick"+sa_postfix);
}
else if ((m_wants_buttjump || m_does_buttjump) && is_big() && !m_water_jump) {
if (m_buttjump_stomp) {
m_sprite->set_action(sa_prefix + "-stomp" + sa_postfix, 1);
set_action(sa_prefix + "-stomp" + sa_postfix, 1);
}
else {
m_sprite->set_action(sa_prefix + "-buttjump" + sa_postfix, 1);
set_action(sa_prefix + "-buttjump" + sa_postfix, 1);
}
}
else if ((m_controller->hold(Control::LEFT) || m_controller->hold(Control::RIGHT)) && m_can_walljump)
{
m_sprite->set_action(sa_prefix+"-walljump"+(m_on_left_wall ? "-left" : "-right"), 1);
set_action(sa_prefix+"-walljump"+(m_on_left_wall ? "-left" : "-right"), 1);
}
else if (!on_ground() || m_fall_mode != ON_GROUND)
{
Expand All @@ -2157,20 +2159,20 @@ Player::draw(DrawingContext& context)
if (m_water_jump && m_dir != m_old_dir)
log_debug << "Obracanko (:" << std::endl;
if (glm::length(m_physic.get_velocity()) < 50.f)
m_sprite->set_action(sa_prefix + "-float" + sa_postfix);
set_action(sa_prefix + "-float" + sa_postfix);
else if (m_water_jump)
m_sprite->set_action(sa_prefix + "-swimjump" + sa_postfix);
set_action(sa_prefix + "-swimjump" + sa_postfix);
else if (m_swimboosting)
m_sprite->set_action(sa_prefix + "-boost" + sa_postfix);
set_action(sa_prefix + "-boost" + sa_postfix);
else
m_sprite->set_action(sa_prefix + "-swim" + sa_postfix);
set_action(sa_prefix + "-swim" + sa_postfix);
}
else
{
if (m_physic.get_velocity_y() > 0)
m_sprite->set_action(sa_prefix + "-fall" + sa_postfix);
set_action(sa_prefix + "-fall" + sa_postfix);
else if (m_physic.get_velocity_y() <= 0)
m_sprite->set_action(sa_prefix + "-jump" + sa_postfix);
set_action(sa_prefix + "-jump" + sa_postfix);
}
}
}
Expand All @@ -2183,34 +2185,34 @@ Player::draw(DrawingContext& context)
m_idle_stage = 0;
m_idle_timer.start(static_cast<float>(TIME_UNTIL_IDLE) / 1000.0f);

m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, Sprite::LOOPS_CONTINUED);
set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, Sprite::LOOPS_CONTINUED);
}
else if (m_idle_timer.check() || m_sprite->animation_done()) {
m_idle_stage++;
if (m_idle_stage >= static_cast<unsigned int>(IDLE_STAGES.size()))
{
m_idle_stage = static_cast<int>(IDLE_STAGES.size()) - 1;
m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix);
set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix);
m_sprite->set_animation_loops(-1);
}
else
{
m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, 1);
set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, 1);
}
}
else {
m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, Sprite::LOOPS_CONTINUED);
set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, Sprite::LOOPS_CONTINUED);
}
}
else
{
if (std::abs(m_physic.get_velocity_x()) >= MAX_RUN_XM-3)
{
m_sprite->set_action(sa_prefix+"-run"+sa_postfix);
set_action(sa_prefix+"-run"+sa_postfix);
}
else
{
m_sprite->set_action(sa_prefix+"-walk"+sa_postfix);
set_action(sa_prefix+"-walk"+sa_postfix);
}
}
}
Expand Down Expand Up @@ -3063,7 +3065,7 @@ Player::remove_collected_key(Key* key)
void
Player::register_class(ssq::VM& vm)
{
ssq::Class cls = vm.addAbstractClass<Player>("Player", vm.findClass("MovingObject"));
ssq::Class cls = vm.addAbstractClass<Player>("Player", vm.findClass("MovingSprite"));

cls.addFunc<bool, Player, const std::string&>("add_bonus", &Player::add_bonus);
cls.addFunc<bool, Player, const std::string&>("set_bonus", &Player::set_bonus);
Expand Down
7 changes: 3 additions & 4 deletions src/object/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef HEADER_SUPERTUX_OBJECT_PLAYER_HPP
#define HEADER_SUPERTUX_OBJECT_PLAYER_HPP

#include "moving_sprite.hpp"
#include "sprite/sprite_ptr.hpp"
#include "supertux/direction.hpp"
#include "supertux/moving_object.hpp"
Expand All @@ -42,12 +43,12 @@ extern const float TUX_INVINCIBLE_TIME_WARNING;

/**
* @scripting
* @summary This module contains methods controlling the player. (No, SuperTux doesn't use mind control. ""Player"" refers to the type of the player object.)
* @summary This module contains methods controlling the player.
* @instances The first player can be accessed using ""Tux"", or ""sector.Tux"" from the console.
All following players (2nd, 3rd, etc...) can be accessed by ""Tux{index}"".
For example, to access the 2nd player, use ""Tux1"" (or ""sector.Tux1"" from the console).
*/
class Player final : public MovingObject
class Player final : public MovingSprite
{
public:
static void register_class(ssq::VM& vm);
Expand Down Expand Up @@ -580,8 +581,6 @@ class Player final : public MovingObject
std::unique_ptr<ObjectRemoveListener> m_grabbed_object_remove_listener;
bool m_released_object;

SpritePtr m_sprite; /**< The main sprite representing Tux */

float m_swimming_angle;
float m_swimming_accel_modifier;
bool m_water_jump;
Expand Down

0 comments on commit 671da81

Please sign in to comment.