Code Monkey home page Code Monkey logo

ebitick's Introduction

ebitick⏱️

Go Reference

ebitick is a timer system for Ebitengine games.

Why?

Because timing stuff is important and can be done in an easy to use, set-it-and-forget-it kinda way. You can just use time.After(), but that works on an additional goroutine, which can introduce race conditions into the mix. Ebitick, in comparison, works on the same goroutine as the rest of your game.

❗ : Note that ebitick keeps time by counting ticks against the target tickrate (TPS), so it won't work properly if you change the tickrate while timers are running.

How?

go get github.com/solarlune/ebitick

package main

import (
    "fmt"
    "time"
    
    "github.com/hajimehoshi/ebiten/v2"
    "github.com/solarlune/ebitick"
)

type Game struct {
    TimerSystem *ebitick.TimerSystem
}

func NewGame() *Game {

    game := &Game{
        TimerSystem: ebitick.NewTimerSystem(),
    }

    // Below, we specify that after a second has passed,
    // we run the given function literal.
    game.TimerSystem.After(time.Second, func() {
        fmt.Println("A second has passed.")
    })

    // There's also a TimerSystem.AfterTicks() function if you want to use
    // ticks exactly without any human-readable time.Duration conversions.
    
    // The After functions return the created Timer, allowing you to pause it
    // later as necessary or set it to loop, for example. Once the timer elapses,
    // it is removed from the TimerSystem and its state is set to StateFinished,
    // so you can check for that if necessary as well.

    return game

}

func (game *Game) Update() error {

    // We have the TimerSystem update once per game tick, and that's it.
    // If you want to adjust the speed with which timers run, change TimerSystem.Speed.
    game.TimerSystem.Update()

    return nil

}

func (game *Game) Draw(screen *ebiten.Image) {}

func (game *Game) Layout(w, h int) (int, int) { return 320, 240 }

func main() {

    ebiten.SetWindowTitle("Ebitick Minimal Example")
    ebiten.RunGame(NewGame())

}

Shout-outs

Ebitengine's Discord server~

ebitick's People

Contributors

solarlune avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.