Skip to content

Commit

Permalink
remove cutscenes, add some input helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Oct 14, 2023
1 parent 2fd3f4b commit d375795
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 57 deletions.
14 changes: 11 additions & 3 deletions docs/game_data/spel2.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions docs/src/includes/_globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,15 @@ Load another script by id "author/name" and import its `exports` table. Returns:
- `false` if the script was not found but optional is set to true
- an error if the script was not found and the optional argument was not set

### inputs_to_buttons


> Search script examples for [inputs_to_buttons](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=inputs_to_buttons)
#### tuple<float, float, [BUTTON](#BUTTON)> inputs_to_buttons([INPUTS](#INPUTS) inputs)

Converts [INPUTS](#INPUTS) to (x, y, BUTTON)

### intersection


Expand Down Expand Up @@ -1857,6 +1866,15 @@ Warp to a level immediately.
## Input functions


### buttons_to_inputs


> Search script examples for [buttons_to_inputs](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=buttons_to_inputs)
#### [INPUTS](#INPUTS) buttons_to_inputs(float x, float y, [BUTTON](#BUTTON) buttons)

Converts (x, y, BUTTON) to [INPUTS](#INPUTS)

### get_io


Expand Down
10 changes: 1 addition & 9 deletions docs/src/includes/_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,6 @@ Type | Name | Description
int | [get_state_id()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_state_id) |
int | [get_state_id()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_state_id) | Get the `state_id` of a behavior, this is the id that needs to be returned from a behavior's<br/>`get_next_state_id` to enter this state, given that the behavior is added to the movable.

### MovableCutscene

Derived from [CutsceneBehavior](#CutsceneBehavior)


Type | Name | Description
---- | ---- | -----------

### PRNG

[PRNG](#PRNG) (short for Pseudo-Random-Number-Generator) holds 10 128bit wide buffers of memory that are mutated on every generation of a random number.
Expand Down Expand Up @@ -6664,7 +6656,7 @@ nil | [reset_gravity()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q
nil | [set_position(float to_x, float to_y)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=set_position) | Set the absolute position of an entity and offset all rendering related things accordingly to teleport without any interpolation or graphical glitches. If the camera is focused on the entity, it is also moved.
nil | [process_input()](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=process_input) |
[CutsceneBehavior](#CutsceneBehavior) | [cutscene](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=cutscene) |
| [set_cutscene](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=set_cutscene) |
| [clear_cutscene](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=clear_cutscene) |
[VanillaMovableBehavior](#VanillaMovableBehavior) | [get_base_behavior(int state_id)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_base_behavior) | Gets a vanilla behavior from this movable, needs to be called before `clear_behaviors`<br/>but the returned values are still valid after a call to `clear_behaviors`
nil | [add_behavior(MovableBehavior behavior)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=add_behavior) | Add a behavior to this movable, can be either a `VanillaMovableBehavior` or a<br/>`CustomMovableBehavior`
nil | [clear_behavior(MovableBehavior behavior)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=clear_behavior) | Clear a specific behavior of this movable, can be either a `VanillaMovableBehavior` or a<br/>`CustomMovableBehavior`, a behavior with this behaviors `state_id` may be required to<br/>run this movables statemachine without crashing, so add a new one if you are not sure
Expand Down
32 changes: 32 additions & 0 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,38 @@ end
/// Setting to false disables the death screen from popping up for any usual reason, can still load manually
lua["set_death_enabled"] = set_death_enabled;

/// Converts INPUTS to (x, y, BUTTON)
lua["inputs_to_buttons"] = [](INPUTS inputs) -> std::tuple<float, float, BUTTON>
{
float x = 0;
float y = 0;
if (inputs & 0x100)
x = -1;
else if (inputs & 0x200)
x = 1;
if (inputs & 0x400)
y = 1;
else if (inputs & 0x800)
y = -1;
BUTTON buttons = (BUTTON)(inputs & 0x3f);
return std::make_tuple(x, y, buttons);
};

/// Converts (x, y, BUTTON) to INPUTS
lua["buttons_to_inputs"] = [](float x, float y, BUTTON buttons) -> INPUTS
{
INPUTS inputs = buttons;
if (x < 0)
inputs |= 0x100;
else if (x > 0)
inputs |= 0x200;
if (y > 0)
inputs |= 0x400;
else if (y < 0)
inputs |= 0x800;
return inputs;
};

lua.create_named_table("INPUTS", "NONE", 0, "JUMP", 1, "WHIP", 2, "BOMB", 4, "ROPE", 8, "RUN", 16, "DOOR", 32, "MENU", 64, "JOURNAL", 128, "LEFT", 256, "RIGHT", 512, "UP", 1024, "DOWN", 2048);

lua.create_named_table(
Expand Down
48 changes: 3 additions & 45 deletions src/game_api/script/usertypes/entity_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,6 @@
#include "script/lua_backend.hpp" // for LuaBackend
#include "script/safe_cb.hpp" // for make_safe_cb

class CustomCutsceneBehavior : public CutsceneBehavior
{
public:
using CutsceneCb = bool(Movable*, uint32_t);
CustomCutsceneBehavior(sol::function cb);
~CustomCutsceneBehavior();
uint32_t frame{0};
std::function<CutsceneCb> func;
void update(Movable* movable);
};

CustomCutsceneBehavior::CustomCutsceneBehavior(sol::function cb)
{
func = make_safe_cb<CutsceneCb>(std::move(cb));
}

CustomCutsceneBehavior::~CustomCutsceneBehavior()
{
}

void CustomCutsceneBehavior::update(Movable* movable)
{
if (func(movable, ++frame))
{
game_allocator<CutsceneBehavior> a;
a.destroy(movable->cutscene_behavior);
movable->cutscene_behavior = nullptr;
}
}

namespace NEntity
{
void register_usertypes(sol::state& lua)
Expand Down Expand Up @@ -381,25 +351,13 @@ void register_usertypes(sol::state& lua)
movable_type["set_position"] = &Movable::set_position;
movable_type["process_input"] = &Movable::process_input;
movable_type["cutscene"] = sol::readonly(&Movable::cutscene_behavior);
movable_type["set_cutscene"] = [](Movable& movable, sol::optional<sol::function> cb)
movable_type["clear_cutscene"] = [](Movable& movable)
{
if (movable.cutscene_behavior)
{
game_allocator<CutsceneBehavior> a;
a.destroy(movable.cutscene_behavior);
movable.cutscene_behavior = nullptr;
}
if (cb.has_value())
{
game_allocator<CustomCutsceneBehavior> a;
CustomCutsceneBehavior* p = a.allocate(1);
a.construct(p, cb.value());
movable.cutscene_behavior = p;
}
delete movable.cutscene_behavior;
movable.cutscene_behavior = nullptr;
};

lua.new_usertype<CutsceneBehavior>("CutsceneBehavior", sol::no_constructor);
lua.new_usertype<CustomCutsceneBehavior>("MovableCutscene", sol::no_constructor, sol::base_classes, sol::bases<CutsceneBehavior>());

lua["Entity"]["as_entity"] = &Entity::as<Entity>;
lua["Entity"]["as_movable"] = &Entity::as<Movable>;
Expand Down

0 comments on commit d375795

Please sign in to comment.