fishfolk / punchy

A 2.5D side-scroller beatemup, made in Bevy
https://fishfolk.github.io/punchy/player/latest
Other
264 stars 32 forks source link

Scripting MVP #255

Closed zicklag closed 2 years ago

zicklag commented 2 years ago

Closes #253.

zicklag commented 2 years ago

OK, this is ready for review!

We want to wait for https://github.com/jakobhellermann/bevy_mod_js_scripting/pull/23 before merging, but we should have that done within a day.


This PR removes the Health item kind and adds a new Script item kind. Then we use a script to re-implement the health item.

Additionally, you can add scripts to the game without tying them to an item, just by listing them in the .game.yaml. I added a nifty demo script to show how that can be used, and acts as a short tutorial to the basics of the scripting system.

zicklag commented 2 years ago

I just updated to use the latest master version of bevy_mod_js_scripting and this is ready to merge!

It's a big update, though, so I'll wait for @odecay to give at least a casual approval.

A quick summary of what's included:

Also, all scripts can be hot reloaded, so I'm literally able to modify the game while it's running, which is quite fun and incredibly useful!

zicklag commented 2 years ago

Maybe I overdid it, but I pushed a commit with a ton of comments to ops.rs that hopefully helps explain the code.

There are a couple things in there, specifically around JsValueRefs that kind of requires knowing how bevy_mod_js_scripting works to fully understand, and it's not really documented yet because it's brand new. But hopefully the comments give a general overview of what's happening.

I was expecting to see some system that dealt with the events that were being registered like 'ScriptItemThrowEvent. I see they are fired in fighter_state`, am I missing another system that deals with those events on the rust side or is that all thats needed to run the js/ts?

That's what the ItemGetGrabEvents op does in ops.rs. Because the out-of-the-box scripting API from bevy_mod_js_scripting doesn't have a way to read Bevy events, we have to add a custom JsRuntimeOp that adds a punchy.getItemGrabEvents() function to the JavaScript environment.

This function will read all the ScriptItemGrabEvents sent by the code in fighter_state.

Later, we will also need to add another JS op for getting the throw events ( or maybe we should merge both events into the same struct so that we don't need two different kinds of events? :thinking: ).


For anything that the out-of-the-box scripting API doesn't do yet, we can just create our own custom JS ops that can do anything we want, so it's super easy to add new functionality to the scripting interface which is cool.

erlend-sh commented 2 years ago

Does the scripting wiki need any updates?

And is this still valid?

Since the scripting API is in flux, we won't include a tutorial here

zicklag commented 2 years ago

Does the scripting wiki need any updates?

I wrote it as if this was already merged so it's up-to-date as soon as this is merged.

And is this still valid?

Yeah, the demo script I linked to will exist once this is merged and that is heavily commented, acting somewhat like a tutorial.

Now that you mention it, though, I don't like the wording very much. I'll just refer to the demo script as the tutorial. That will also help make sure it stays up-to-date because the game will error when we run it if something is wrong in that file.

erlend-sh commented 2 years ago

I updated the wiki with your Deno explanations over in Jumpy-land 👌

LGTM?

zicklag commented 2 years ago

Looks great, and good idea, thanks!

odecay commented 2 years ago

bors merge

bors[bot] commented 2 years ago

Build failed:

zicklag commented 2 years ago

bors retry

bors[bot] commented 2 years ago

Canceled.

zicklag commented 2 years ago

bors retry

bors[bot] commented 2 years ago

Build succeeded: