Skip to content

Commit

Permalink
rewrote input code
Browse files Browse the repository at this point in the history
rather than having keybinds that are accessed by the code in multiple
places, a single inputHandler struct has the properties need to be read
stored and calculated in 1 place.
  • Loading branch information
moltenwolfcub committed Aug 23, 2023
1 parent f1c4f42 commit ab5f150
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 96 deletions.
29 changes: 4 additions & 25 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Game struct {
view Viewport

renderer Renderer
keys Keybinds
input InputHandler

timeHud TextElement
player Player
Expand All @@ -33,11 +33,10 @@ func NewGame() Game {
startTime := 10

g := Game{
player: NewPlayer(),
view: NewViewport(),
renderer: NewRenderer(),
input: NewInputHandler(),
time: Time(TPGM * 60 * startTime),
keys: NewKeybinds(),

trees: []Tree{},
inclines: []Incline{
Expand All @@ -53,6 +52,8 @@ func NewGame() Game {
}},
},
}
g.player = NewPlayer(&g)

g.berries = []Berry{NewBerry(image.Pt(256, -128), g.time)}

g.timeHud = TextElement{
Expand Down Expand Up @@ -95,33 +96,11 @@ func (g *Game) Update() error {
g.berries[i].Update(g.time)
}

g.HandleInput()
g.player.Update(collideables, climbables, rivers)
g.view.UpdatePosition(g.player)
return nil
}

func (g *Game) HandleInput() {
var delta = image.Point{0, 0}

if g.keys.Forwards.Triggered() {
delta.Y -= 1
}
if g.keys.Backwards.Triggered() {
delta.Y += 1
}
if g.keys.Left.Triggered() {
delta.X -= 1
}
if g.keys.Right.Triggered() {
delta.X += 1
}
g.player.Delta = delta

g.player.Climbing = g.keys.Climb.Triggered()
g.player.RiverJumping = g.keys.RiverJump.Triggered()
}

func (g Game) Draw(screen *ebiten.Image) {
mapElements := []DepthAwareDrawable{
g.player,
Expand Down
49 changes: 49 additions & 0 deletions game/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package game

import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)

type InputHandler struct {
forwards ebiten.Key
backwards ebiten.Key
left ebiten.Key
right ebiten.Key
jump ebiten.Key
climb ebiten.Key
}

func NewInputHandler() InputHandler {
return InputHandler{
forwards: ebiten.KeyW,
backwards: ebiten.KeyS,
left: ebiten.KeyA,
right: ebiten.KeyD,
jump: ebiten.KeySpace,
climb: ebiten.KeySpace,
}
}

func (p InputHandler) ForwardsImpulse() float64 {
return calculateImpulse(ebiten.IsKeyPressed(p.forwards), ebiten.IsKeyPressed(p.backwards))
}
func (p InputHandler) LeftImpulse() float64 {
return calculateImpulse(ebiten.IsKeyPressed(p.left), ebiten.IsKeyPressed(p.right))
}
func (p InputHandler) IsJumping() bool {
return inpututil.IsKeyJustPressed(p.jump)
}
func (p InputHandler) IsClimbing() bool {
return ebiten.IsKeyPressed(p.jump)
}

func calculateImpulse(positive bool, negative bool) float64 {
if positive == negative {
return 0
}
if positive {
return 1
}
return -1
}
61 changes: 0 additions & 61 deletions game/keybinds.go

This file was deleted.

19 changes: 9 additions & 10 deletions game/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ const (
)

type Player struct {
Delta image.Point
game *Game
hitbox image.Rectangle
Climbing bool
RiverJumping bool
currentMoveSpeed float64
}

func NewPlayer() Player {
func NewPlayer(game *Game) Player {
width, height := assets.Player.Bounds().Size().X, assets.Player.Bounds().Size().Y
return Player{
game: game,
hitbox: image.Rectangle{
Min: image.Point{-100, -100},
Max: image.Point{width - 100, height - 100},
Expand Down Expand Up @@ -97,7 +96,7 @@ func (p *Player) Update(collidables []HasHitbox, climbables []Climbable, rivers
}

func (p *Player) handleInteractions(interactables []HasHitbox) {
if p.RiverJumping {
if p.game.input.IsJumping() {

newPos, found := p.GetSmallestJump(interactables)

Expand Down Expand Up @@ -227,7 +226,7 @@ func (p Player) calculateMovementSpeed(currentClimable Climbable) (speed float64

func (p *Player) tryClimb(currentClimable Climbable) {
if currentClimable != nil {
if p.Climbing {
if p.game.input.IsClimbing() {
p.hitbox = p.hitbox.Sub(image.Point{
Y: int(p.currentMoveSpeed),
})
Expand Down Expand Up @@ -255,8 +254,8 @@ func (p *Player) movePlayer(collidables []HasHitbox, climbables []Climbable) {
steps := int(scalar)
stepSize := scalar / float64(steps)

x := image.Point{X: int(float64(p.Delta.X) * stepSize)}
y := image.Point{Y: int(float64(p.Delta.Y) * stepSize)}
x := image.Point{X: int(float64(-p.game.input.LeftImpulse()) * stepSize)}
y := image.Point{Y: int(float64(-p.game.input.ForwardsImpulse()) * stepSize)}

climbingPreMove := p.findCurrentClimable(climbables) == nil

Expand All @@ -272,11 +271,11 @@ func (p *Player) movePlayer(collidables []HasHitbox, climbables []Climbable) {
continue
}
p.hitbox = p.hitbox.Add(y)
if p.Climbing && p.findCurrentClimable(climbables) != nil && y.Y <= 0 {
if p.game.input.IsClimbing() && p.findCurrentClimable(climbables) != nil && y.Y <= 0 {
//if hitting the bottom of a climbable while trying to climb don't fix collisions
continue
}
if !p.Climbing && p.findCurrentClimable(climbables) != nil && y.Y >= 0 {
if !p.game.input.IsClimbing() && p.findCurrentClimable(climbables) != nil && y.Y >= 0 {
//if hitting the top of a climbable and not climbing don't fix collisions
continue
}
Expand Down

0 comments on commit ab5f150

Please sign in to comment.