-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_control.go
91 lines (77 loc) · 1.92 KB
/
player_control.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"math"
"time"
"github.com/veandco/go-sdl2/sdl"
)
type keyboardMover struct {
container *element
speed float64
sr *spriteRenderer
}
func newKeyboardMover(container *element, speed float64) *keyboardMover {
return &keyboardMover{
container: container,
speed: speed,
sr: container.getComponent(&spriteRenderer{}).(*spriteRenderer),
}
}
func (mover *keyboardMover) onUpdate() error {
keys := sdl.GetKeyboardState()
cont := mover.container
if keys[sdl.SCANCODE_LEFT] == 1 {
newPos := cont.position.x - (mover.sr.width / 2.0)
if newPos > 0 {
cont.position.x -= mover.speed * delta
}
} else if keys[sdl.SCANCODE_RIGHT] == 1 {
if cont.position.x+(mover.sr.width/2.0) < screenWidth {
cont.position.x += mover.speed * delta
}
}
return nil
}
func (mover *keyboardMover) onDraw(renderer *sdl.Renderer) error {
return nil
}
func (mover *keyboardMover) onCollision(other *element) error {
return nil
}
type keyboardShooter struct {
container *element
cooldown time.Duration
lastShot time.Time
}
func newKeyboardShooter(container *element, cooldown time.Duration) *keyboardShooter {
return &keyboardShooter{
container: container,
cooldown: cooldown,
}
}
func (shooter *keyboardShooter) onUpdate() error {
keys := sdl.GetKeyboardState()
pos := shooter.container.position
if keys[sdl.SCANCODE_SPACE] == 1 {
if time.Since(shooter.lastShot) < shooter.cooldown {
return nil
}
shooter.shoot(pos.x+25, pos.y-20)
shooter.shoot(pos.x-25, pos.y-20)
shooter.lastShot = time.Now()
}
return nil
}
func (shooter *keyboardShooter) onDraw(renderer *sdl.Renderer) error {
return nil
}
func (shooter *keyboardShooter) onCollision(other *element) error {
return nil
}
func (shooter *keyboardShooter) shoot(x, y float64) {
if bul, ok := getBulletFromPool(); ok {
bul.active = true
bul.position.x = x
bul.position.y = y
bul.rotation = 270 * (math.Pi / 180)
}
}