decentraland / kernel-legacy

🌎 Explore Decentraland from a web browser
https://decentraland.org
Apache License 2.0
20 stars 7 forks source link

Helper time-related components #76

Open nearnshaw opened 5 years ago

nearnshaw commented 5 years ago

Create three new components Timer, TimeOut, WillExpire to help developers deal with the timing of code easily. All three are closely related.

Timer:

A component that has two fields: a lambda to execute (like the OnPointerDown component) and an interval in milliseconds between each execution.

This makes it easy for the developer to run a same function at regular intervals, without needing to write a system for it.

Example:

let ent = new Entity()
ent.addComponent(new Timer(e => {
    ambientSoundClip.play()
  }, 1000))
engine.addEntity(ent)

TimeOut:

A component that has two fields: a lambda to execute (like the OnPointerDown component) and a wait interval before executing this lambda once.

After the function is executed, the component is automatically removed from the entity.

This makes it easy for the developer to delay the execution of a function, without having to write a system that first waits.

Example:

let ent = new Entity()
ent.addComponent(new TimeOut(e => {
    log("thanks for waiting")
}, 100000))
engine.addEntity(ent)

Of course, this component is more useful if it doesn't start as part of the scene, but it's added to an entity when something happens. For example:

Example2:

let ent = new Entity()
ent.addComponent(new OnPointerDown( e => {
        ent.addComponent(new TimeOut(e => {
                log("you clicked one second ago")
        }, 1000))
}))
engine.addEntity(ent)

WillExpire

A component that has a single field: milliseconds before removing an entity from the engine. When the time has passed, the entity is removed from the engine automatically.

This makes it easy to have things in the scene that expire automatically after X time. For example, a floating text that appears over a collected item saying how many points it gave you. Or if I have an NPC enemy that plays a 'die' animation and I want it to play that animation before making it disappear.

Example:

let ent = new Entity()
ent.addComponent(new WillExpire(1000))
ent.addComponent(new TextShape("this message will self-destruct"))
engine.addEntity(ent)
nearnshaw commented 5 years ago

Example scenes that use similar components:

Timer: https://github.com/decentraland-scenes/Hummingbirds

TimeOut: https://github.com/decentraland-scenes/Gnark-patrol

WillExpire: https://github.com/decentraland-scenes/Tower-defense