-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
f1c4f42
commit ab5f150
Showing
4 changed files
with
62 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters