-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard_shooter.go
54 lines (42 loc) · 1.02 KB
/
keyboard_shooter.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 (
"math"
"time"
"github.com/veandco/go-sdl2/sdl"
)
type KeyboardShooter struct {
container *Element
cooldown time.Duration
lastShot time.Time
}
func CreateKeyboardShooter(container *Element, cooldown time.Duration) *KeyboardShooter {
return &KeyboardShooter{
container: container,
cooldown: cooldown}
}
func (shooter *KeyboardShooter) onDraw(renderer *sdl.Renderer) error {
return nil
}
func (shooter *KeyboardShooter) onUpdate() error {
keys := sdl.GetKeyboardState()
pos := shooter.container.frame.p
if keys[sdl.SCANCODE_SPACE] == 1 {
if time.Since(shooter.lastShot) >= shooter.cooldown {
shooter.shoot(pos.x+6, pos.y-2)
shooter.shoot(pos.x-6, pos.y-2)
shooter.lastShot = time.Now()
}
}
return nil
}
func (shooter *KeyboardShooter) onCollide(other *Element) error {
return nil
}
func (shooter *KeyboardShooter) shoot(x, y float64) {
if bul, ok := BulletFromPool(); ok {
bul.active = true
bul.frame.p.x = x
bul.frame.p.y = y
bul.rotation = 270 * (math.Pi / 180)
}
}