-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard_mover.go
54 lines (44 loc) · 1.18 KB
/
keyboard_mover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"github.com/veandco/go-sdl2/sdl"
)
type KeyboardMover struct {
container *Element
speed float64
sr *SpriteRenderer
}
func CreateKeyboardMover(container *Element, speed float64) *KeyboardMover {
return &KeyboardMover{
container: container,
speed: speed,
sr: container.getComponent(&SpriteRenderer{}).(*SpriteRenderer)}
}
func (mover *KeyboardMover) onDraw(renderer *sdl.Renderer) error {
return nil
}
func (mover *KeyboardMover) onUpdate() error {
keys := sdl.GetKeyboardState()
pos := mover.container.frame.p
if keys[sdl.SCANCODE_LEFT] == 1 {
if pos.x-(mover.sr.frame.s.w/2.0) > 0 {
mover.container.frame.p.x -= mover.speed
}
} else if keys[sdl.SCANCODE_RIGHT] == 1 {
if pos.x+(mover.sr.frame.s.w/2.0) < SCREEN_WIDTH {
mover.container.frame.p.x += mover.speed
}
}
if keys[sdl.SCANCODE_UP] == 1 {
if pos.y-(mover.sr.frame.s.h/2.0) > 0 {
mover.container.frame.p.y -= mover.speed
}
} else if keys[sdl.SCANCODE_DOWN] == 1 {
if pos.y+(mover.sr.frame.s.h/2.0) < SCREEN_HEIGHT {
mover.container.frame.p.y += mover.speed
}
}
return nil
}
func (mover *KeyboardMover) onCollide(other *Element) error {
return nil
}