Fixed delta time implementation.

This commit is contained in:
Andrey Parhomenko 2023-02-18 02:17:51 +05:00
parent be9c3dc619
commit 8d8ce41e71
2 changed files with 10 additions and 4 deletions

View file

@ -21,7 +21,7 @@ func NewPlayer() *Player {
Object: &gx.Object{ Object: &gx.Object{
T: gx.Transform { T: gx.Transform {
P: gx.Vector{1, 1}, P: gx.Vector{1, 1},
S: gx.Vector{1, 1}, S: gx.Vector{5, 5},
}, },
}, },
Image: playerImg, Image: playerImg,
@ -30,7 +30,7 @@ func NewPlayer() *Player {
} }
func (p *Player) Update(e *gx.Engine) { func (p *Player) Update(e *gx.Engine) {
p.Sprite.Object.T.P.Y += 0.1 p.Sprite.Object.T.P.Y += 40 * e.DT()
} }
func main() { func main() {

View file

@ -70,15 +70,19 @@ func (e *Engine) AddBehaver(b Behaver) {
e.behavers = append(e.behavers, b) e.behavers = append(e.behavers, b)
} }
func (e *engine) setNewDT() {
e.dt = time.Since(e.lastTime).Seconds()
e.lastTime = time.Now()
}
func (e *engine) Update() error { func (e *engine) Update() error {
eng := (*Engine)(e) eng := (*Engine)(e)
e.dt = time.Since(e.lastTime).Seconds() e.setNewDT()
for _, v := range eng.behavers { for _, v := range eng.behavers {
v.Update(eng) v.Update(eng)
//fmt.Println(v) //fmt.Println(v)
} }
e.lastTime = time.Now()
return nil return nil
} }
@ -106,6 +110,8 @@ func (e *Engine) Run() error {
ebiten.SetWindowTitle(e.wcfg.Title) ebiten.SetWindowTitle(e.wcfg.Title)
ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height) ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height)
e.lastTime = time.Now()
return ebiten.RunGame((*engine)(e)) return ebiten.RunGame((*engine)(e))
} }