Skip to main content

News

Topic: EventEmitter for miniSphere (Read 6651 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
EventEmitter for miniSphere
I'm working on a relatively simple (but hopefully useful) event system for miniSphere influenced by Node.js's EventEmitter class, so if you're familiar with that, this should be fairly simple to get used to.

Some examples:
Code: (JavaScript) [Select]
import { Thread } from 'sphere-runtime';
import { EventEmitter } from 'events';

const maxListeners = 32;
const escOnlyOnce = true;
let ei = new EventEmitter(maxListeners);

export default class Game extends Thread {
    constructor() {
        super();
        ei.addListener("escEvent", () => {
            Sphere.shutDown();
        }, escOnlyOnce);
    }

    on_update() {
        if(kb.isPressed(Key.Escape)) ei.emit("escEvent");
     }  
}

The `event#addListener` function also takes an optional parameter that emits the listener when it equals true or is a function that returns true.
Code: (JavaScript) [Select]
ei.addListener("escEvent", Sphere.shutDown, escOnlyOnce, () => {
    return kb.isPressed(Key.Escape);
});

I'm also working on a class for handling key press/release events

GitHub repo: https://github.com/eggbertx/eventemitter-sphere
  • Last Edit: September 05, 2019, 08:34:44 pm by Eggbertx

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: EventEmitter for miniSphere
Reply #1
I like this idea, it's great on turn based tactic style games, and other event driven behaviors.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: EventEmitter for miniSphere
Reply #2
I actually got the idea while working on my IRC client module to simplify handling IRC events, and figured it could be applied to many other projects. I created a GitHub repo for it and added a link to the OP.