Skip to content

Commit

Permalink
18/8/21 7:45 PM
Browse files Browse the repository at this point in the history
  • Loading branch information
eboatwright committed Aug 19, 2021
1 parent 2c868c8 commit a43569e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 1 deletion.
Binary file modified .DS_Store
Binary file not shown.
55 changes: 55 additions & 0 deletions builtInComponentsAndSystems/animator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package builtInComponentsAndSystems


import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/eboatwright/pearl"
)


type Animation struct {
Frames []int
Speed int
}

type Animator struct {
Animations []Animation
CurrentAnimation int
CurrentAnimationIndex int
AnimationTimer int
}
func (a *Animator) ID() string { return "animator" }

func (a *Animator) ChangeAnimation(index int) {
if a.CurrentAnimation != index {
a.CurrentAnimation = index
a.CurrentAnimationIndex = 0
a.AnimationTimer = 0
}
}


type AnimationSystem struct {}

func (as *AnimationSystem) Update(entity *pearl.Entity, scene *pearl.Scene) {
a := entity.GetComponent("animator").(*Animator)
i := entity.GetComponent("image").(*Image)

a.AnimationTimer--
if a.AnimationTimer <= 0 {
a.AnimationTimer = a.Animations[a.CurrentAnimation].Speed
a.CurrentAnimationIndex++

if a.CurrentAnimationIndex >= len(a.Animations[a.CurrentAnimation].Frames) {
a.CurrentAnimationIndex = 0
}

i.SourcePos.X = float64(a.Animations[a.CurrentAnimation].Frames[a.CurrentAnimationIndex]) * i.Size.X
}
}

func (as *AnimationSystem) Draw(entity *pearl.Entity, scene *pearl.Scene, screen *ebiten.Image, options *ebiten.DrawImageOptions) {}

func (as *AnimationSystem) GetRequirements() []string {
return []string { "animator", "image" }
}
Binary file modified examples/.DS_Store
Binary file not shown.
Binary file modified examples/2-platformer/.DS_Store
Binary file not shown.
Binary file modified examples/2-platformer/data/.DS_Store
Binary file not shown.
Binary file modified examples/2-platformer/data/img/wizard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions examples/2-platformer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ func onStart() {
Image: pearl.LoadImage("data/img/wizard.png"),
Size: pearl.Vector2 { 16, 16 },
},
&bicas.Animator {
Animations: []bicas.Animation {
bicas.Animation {
Frames: []int { 0 },
Speed: 1,
},
bicas.Animation {
Frames: []int { 0, 1 },
Speed: 8,
},
},
},
&bicas.Rigidbody {
Friction: pearl.Vector2 { 0.8, 1 },
Gravity: pearl.Vector2 { 0, 0.4 },
Expand Down Expand Up @@ -89,6 +101,7 @@ func onStart() {

&bicas.LevelRenderer { },
&bicas.ImageRenderer { },
&bicas.AnimationSystem { },
})
}

Expand Down
6 changes: 5 additions & 1 deletion examples/2-platformer/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (ps *PlayerSystem) Update(entity *pearl.Entity, scene *pearl.Scene) {
t := entity.GetComponent("transform").(*bicas.Transform)
rb := entity.GetComponent("rigidbody").(*bicas.Rigidbody)
p := entity.GetComponent("player").(*Player)
anim := entity.GetComponent("animator").(*bicas.Animator)

xInput := pearl.GetInputAxis(
[]ebiten.Key { ebiten.KeyA, ebiten.KeyLeft },
Expand All @@ -30,6 +31,9 @@ func (ps *PlayerSystem) Update(entity *pearl.Entity, scene *pearl.Scene) {

if xInput != 0 {
t.Scale.X = float64(xInput)
anim.ChangeAnimation(1)
} else {
anim.ChangeAnimation(0)
}

rb.Velocity.X += float64(xInput) * p.MoveSpeed
Expand All @@ -46,5 +50,5 @@ func (ps *PlayerSystem) Update(entity *pearl.Entity, scene *pearl.Scene) {
func (ps *PlayerSystem) Draw(entity *pearl.Entity, scene *pearl.Scene, screen *ebiten.Image, options *ebiten.DrawImageOptions) {}

func (ps *PlayerSystem) GetRequirements() []string {
return []string { "transform", "rigidbody", "player" }
return []string { "transform", "rigidbody", "animator", "player" }
}

0 comments on commit a43569e

Please sign in to comment.