FishyW / engine

boring stuff nothin to see here
MIT License
0 stars 0 forks source link

User Script API #4

Open FishyW opened 9 months ago

FishyW commented 9 months ago

JavaScript API

JavaScript calls all Rust functions that start with init_ when the game is loaded by switching to play mode or the game is reloaded using Ctrl + R. This allows multiple entry points into the program.

Receiver/Interceptor API

// I prefer if there's no function parameters, coz more concise
// but probably ur gonna disagree, so prob we're adding the parameters 
#[receiver(MyObject, MyEvent)]
fn receive() {
    // access object properties
    self.<prop>;
    // access event properties
    event.<prop>;
}

#[interceptor(MyManager, MyEvent)]
fn intercept() {
    // manager properties
    self.<prop>;
    // event properties
    event.<prop>;

       // (optional) event metadata includes sender, event target(s), etc. 
       metadata.sender;

    // intercept() needs to return an event
    event
}

// interceptor can also return a destroyEvent,
// which destroys the event
fn intercept()  {destroy(event)}

// or it can return a different event
fn intercept()  {
    // note "event" is the original event
    MyOtherEvent {
        receiver: event.receiver(),
        // or use the default!() macro -> expands to ...Default::default()
        ...Default::default()
    }
}

Asset API

#[asset(object)]
struct MyObject {prop1: String, prop2: i32}

// components
#[asset(component)]
struct MyComponent {
    pub fly_height: i32
}

impl MyComponent {
   pub fn fly() {}
}

// component API
#[asset(object)]
#[include(MyComponent, MyComponent2)] // -> auto generates a my_component attribute
struct MyObject {}

// then inside of a receiver
self.my_component.fly();

#[asset(manager)]
struct MyManager {}

#[asset(event)]
struct MyEvent {}

// sending an event
event.send<MyObject>();

// broadcasting an event
event.broadcast();

// specify action attributes here
#[asset(action)] 
struct MyAction {}

#[action(MyAction)] 
fn action() {
    let x = self.attr1;
}

Conditionals

A conditional is a function that accepts an event and returns Some(Action). This is useful for the event manager. It is called a conditional since it is used to check for certain event attributes, and if these event attributes are equal to a certain value, the corresponding action is returned, else None is returned.

#[conditional(MyEvent, MyAction)]
fn my_condition() {
     if (event.prop1 == "sth" && event.prop2 == "sth") {
             let action = MyAction {prop1: "sth", prop2: "sth"};
             Some(action)
     } else {
           None
      }
}

Others

// to rotate an object
self.transform.rotate(360);

// set and get the transform x value
self.transform.x = self.transform.x + 1;

// creating events
let event = MyEvent {default!()};
// sending an event to an object instance
event.send(instance);
// sending an event to all instances of an Object Type
event.send(Object::Address);
// sending an event to multiple objects/instances
event.send(Address::from([Object::Address, Object2::Address, object]));
// or using the address! macro, note !object means "object" is an object instance not an object type
event.send(address![Object, Object2, !object]);
// broadcast an event
event.broadcast();

// create a new object instance
let object = MyObject {prop1: 2, prop2: 4, default!()};
// instantiate object into the scene, this tells Pixi.js to render this object
object.instantiate();
// destroy the object
object.destroy();
FishyW commented 9 months ago

Implementation Suggestions:

  1. Don't use JSON, but auto generate a scene script (for each scene) and put it all there.
  2. Don't confuse yourself with the difference between an object type or an object instance. Multiple instances can be made from the same object type.
  3. All events need to pass through the event router, the event router then can do some background stuff before and after the event is received (like updating some data for example).
  4. Use macros to the maximum, generate as much code as you can with macros.
  5. Can't think of more today.