mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
Add input
This commit is contained in:
parent
c039c13acd
commit
7fd54f6bb3
@ -3,7 +3,7 @@ package blocks
|
|||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/graphics"
|
"github.com/hajimehoshi/ebiten/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/graphics/matrix"
|
"github.com/hajimehoshi/ebiten/graphics/matrix"
|
||||||
"github.com/hajimehoshi/ebiten/ui"
|
"github.com/hajimehoshi/ebiten/input"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
@ -64,26 +64,26 @@ func (s *GameScene) Update(state *GameState) {
|
|||||||
y := s.currentPieceY
|
y := s.currentPieceY
|
||||||
angle := s.currentPieceAngle
|
angle := s.currentPieceAngle
|
||||||
moved := false
|
moved := false
|
||||||
if state.Input.StateForKey(ui.KeySpace) == 1 {
|
if state.Input.StateForKey(input.KeySpace) == 1 {
|
||||||
s.currentPieceAngle = s.field.RotatePieceRight(piece, x, y, angle)
|
s.currentPieceAngle = s.field.RotatePieceRight(piece, x, y, angle)
|
||||||
moved = angle != s.currentPieceAngle
|
moved = angle != s.currentPieceAngle
|
||||||
}
|
}
|
||||||
if l := state.Input.StateForKey(ui.KeyLeft); l == 1 || (10 <= l && l%2 == 0) {
|
if l := state.Input.StateForKey(input.KeyLeft); l == 1 || (10 <= l && l%2 == 0) {
|
||||||
s.currentPieceX = s.field.MovePieceToLeft(piece, x, y, angle)
|
s.currentPieceX = s.field.MovePieceToLeft(piece, x, y, angle)
|
||||||
moved = x != s.currentPieceX
|
moved = x != s.currentPieceX
|
||||||
}
|
}
|
||||||
if r := state.Input.StateForKey(ui.KeyRight); r == 1 || (10 <= r && r%2 == 0) {
|
if r := state.Input.StateForKey(input.KeyRight); r == 1 || (10 <= r && r%2 == 0) {
|
||||||
s.currentPieceX = s.field.MovePieceToRight(piece, x, y, angle)
|
s.currentPieceX = s.field.MovePieceToRight(piece, x, y, angle)
|
||||||
moved = y != s.currentPieceX
|
moved = y != s.currentPieceX
|
||||||
}
|
}
|
||||||
if d := state.Input.StateForKey(ui.KeyDown); (d-1)%2 == 0 {
|
if d := state.Input.StateForKey(input.KeyDown); (d-1)%2 == 0 {
|
||||||
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
|
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
|
||||||
moved = y != s.currentPieceY
|
moved = y != s.currentPieceY
|
||||||
}
|
}
|
||||||
if moved {
|
if moved {
|
||||||
s.landingCount = 0
|
s.landingCount = 0
|
||||||
} else if !s.field.PieceDroppable(piece, x, y, angle) {
|
} else if !s.field.PieceDroppable(piece, x, y, angle) {
|
||||||
if 0 < state.Input.StateForKey(ui.KeyDown) {
|
if 0 < state.Input.StateForKey(input.KeyDown) {
|
||||||
s.landingCount += 10
|
s.landingCount += 10
|
||||||
} else {
|
} else {
|
||||||
s.landingCount++
|
s.landingCount++
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
package blocks
|
package blocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/ui"
|
"github.com/hajimehoshi/ebiten/input"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Input struct {
|
type Input struct {
|
||||||
states map[ui.Key]int
|
states map[input.Key]int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInput() *Input {
|
func NewInput() *Input {
|
||||||
states := map[ui.Key]int{}
|
states := map[input.Key]int{}
|
||||||
for key := ui.Key(0); key < ui.KeyMax; key++ {
|
for key := input.Key(0); key < input.KeyMax; key++ {
|
||||||
states[key] = 0
|
states[key] = 0
|
||||||
}
|
}
|
||||||
return &Input{
|
return &Input{
|
||||||
@ -18,13 +18,13 @@ func NewInput() *Input {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) StateForKey(key ui.Key) int {
|
func (i *Input) StateForKey(key input.Key) int {
|
||||||
return i.states[key]
|
return i.states[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) Update() {
|
func (i *Input) Update() {
|
||||||
for key := range i.states {
|
for key := range i.states {
|
||||||
if !ui.IsKeyPressed(key) {
|
if !input.IsKeyPressed(key) {
|
||||||
i.states[key] = 0
|
i.states[key] = 0
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package blocks
|
|||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/graphics"
|
"github.com/hajimehoshi/ebiten/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/graphics/matrix"
|
"github.com/hajimehoshi/ebiten/graphics/matrix"
|
||||||
"github.com/hajimehoshi/ebiten/ui"
|
"github.com/hajimehoshi/ebiten/input"
|
||||||
"image/color"
|
"image/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ func NewTitleScene() *TitleScene {
|
|||||||
|
|
||||||
func (s *TitleScene) Update(state *GameState) {
|
func (s *TitleScene) Update(state *GameState) {
|
||||||
s.count++
|
s.count++
|
||||||
if state.Input.StateForKey(ui.KeySpace) == 1 {
|
if state.Input.StateForKey(input.KeySpace) == 1 {
|
||||||
state.SceneManager.GoTo(NewGameScene())
|
state.SceneManager.GoTo(NewGameScene())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package ui
|
package input
|
||||||
|
|
||||||
type Key int
|
type Key int
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
|||||||
glfw "github.com/go-gl/glfw3"
|
glfw "github.com/go-gl/glfw3"
|
||||||
"github.com/hajimehoshi/ebiten/graphics"
|
"github.com/hajimehoshi/ebiten/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/graphics/opengl"
|
||||||
|
"github.com/hajimehoshi/ebiten/input"
|
||||||
"github.com/hajimehoshi/ebiten/ui"
|
"github.com/hajimehoshi/ebiten/ui"
|
||||||
"image"
|
"image"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -29,7 +30,7 @@ func NewCanvas(width, height, scale int, title string) *Canvas {
|
|||||||
funcsDone: make(chan struct{}),
|
funcsDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.SetKeyboard(canvas.keyboard)
|
input.SetKeyboard(canvas.keyboard)
|
||||||
graphics.SetTextureFactory(canvas)
|
graphics.SetTextureFactory(canvas)
|
||||||
|
|
||||||
// For retina displays, recalculate the scale with the framebuffer size.
|
// For retina displays, recalculate the scale with the framebuffer size.
|
||||||
|
@ -2,30 +2,30 @@ package glfw
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
glfw "github.com/go-gl/glfw3"
|
glfw "github.com/go-gl/glfw3"
|
||||||
"github.com/hajimehoshi/ebiten/ui"
|
"github.com/hajimehoshi/ebiten/input"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Keyboard struct {
|
type Keyboard struct {
|
||||||
pressedKeys map[ui.Key]struct{}
|
pressedKeys map[input.Key]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewKeyboard() *Keyboard {
|
func NewKeyboard() *Keyboard {
|
||||||
return &Keyboard{
|
return &Keyboard{
|
||||||
pressedKeys: map[ui.Key]struct{}{},
|
pressedKeys: map[input.Key]struct{}{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) IsKeyPressed(key ui.Key) bool {
|
func (k *Keyboard) IsKeyPressed(key input.Key) bool {
|
||||||
_, ok := k.pressedKeys[key]
|
_, ok := k.pressedKeys[key]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
var glfwKeyCodeToKey = map[glfw.Key]ui.Key{
|
var glfwKeyCodeToKey = map[glfw.Key]input.Key{
|
||||||
glfw.KeySpace: ui.KeySpace,
|
glfw.KeySpace: input.KeySpace,
|
||||||
glfw.KeyLeft: ui.KeyLeft,
|
glfw.KeyLeft: input.KeyLeft,
|
||||||
glfw.KeyRight: ui.KeyRight,
|
glfw.KeyRight: input.KeyRight,
|
||||||
glfw.KeyUp: ui.KeyUp,
|
glfw.KeyUp: input.KeyUp,
|
||||||
glfw.KeyDown: ui.KeyDown,
|
glfw.KeyDown: input.KeyDown,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) update(window *glfw.Window) {
|
func (k *Keyboard) update(window *glfw.Window) {
|
||||||
|
Loading…
Reference in New Issue
Block a user