-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysteminput.go
116 lines (103 loc) · 3.05 KB
/
systeminput.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package zelduh
import (
"github.com/faiface/pixel/pixelgl"
)
type inputEntity struct {
*ComponentMovement
*ComponentIgnore
*ComponentDash
}
// SystemInput is a custom system for detecting collisions and what to do when they occur
type SystemInput struct {
Win *pixelgl.Window
playerEntity inputEntity
playerEnabled bool
sword inputEntity
arrow inputEntity
}
// DisablePlayer disables player input
func (s *SystemInput) DisablePlayer() {
s.playerEnabled = false
}
// EnablePlayer enables player input
func (s *SystemInput) EnablePlayer() {
s.playerEnabled = true
}
// AddEntity adds an entity to the system
func (s *SystemInput) AddEntity(entity Entity) {
r := inputEntity{
ComponentMovement: entity.ComponentMovement,
ComponentDash: entity.ComponentDash,
ComponentIgnore: entity.ComponentIgnore,
}
switch entity.Category {
case CategoryPlayer:
s.playerEntity = r
case CategorySword:
s.sword = r
case CategoryArrow:
s.arrow = r
}
}
// Update checks for player input
func (s SystemInput) Update() {
if !s.playerEnabled {
return
}
win := s.Win
player := s.playerEntity
movingSpeed := player.ComponentMovement.MaxSpeed
player.ComponentMovement.LastDirection = player.ComponentMovement.Direction
if win.Pressed(pixelgl.KeyUp) {
player.ComponentMovement.Speed = movingSpeed
player.ComponentMovement.Direction = DirectionUp
} else if win.Pressed(pixelgl.KeyRight) {
player.ComponentMovement.Speed = movingSpeed
player.ComponentMovement.Direction = DirectionRight
} else if win.Pressed(pixelgl.KeyDown) {
player.ComponentMovement.Speed = movingSpeed
player.ComponentMovement.Direction = DirectionDown
} else if win.Pressed(pixelgl.KeyLeft) {
player.ComponentMovement.Speed = movingSpeed
player.ComponentMovement.Direction = DirectionLeft
} else {
player.ComponentMovement.Speed = 0
}
// attack with sword
s.sword.ComponentMovement.Direction = player.ComponentMovement.Direction
if win.Pressed(pixelgl.KeyF) {
s.sword.ComponentMovement.Speed = 1.0
s.sword.ComponentIgnore.Value = false
} else {
s.sword.ComponentMovement.Speed = 0
s.sword.ComponentIgnore.Value = true
}
// fire arrow
if s.arrow.ComponentMovement.RemainingMoves == 0 {
s.arrow.ComponentMovement.Direction = player.ComponentMovement.Direction
if win.Pressed(pixelgl.KeyG) {
s.arrow.ComponentMovement.Speed = 7.0
s.arrow.ComponentMovement.RemainingMoves = 100
s.arrow.ComponentIgnore.Value = false
} else {
s.arrow.ComponentMovement.Speed = 0
s.arrow.ComponentMovement.RemainingMoves = 0
s.arrow.ComponentIgnore.Value = true
}
} else {
s.arrow.ComponentMovement.RemainingMoves--
}
// dashing
if !win.Pressed(pixelgl.KeyF) && win.Pressed(pixelgl.KeySpace) {
if s.playerEntity.ComponentDash.Charge < s.playerEntity.ComponentDash.MaxCharge {
s.playerEntity.ComponentDash.Charge++
s.sword.ComponentMovement.Speed = 0
s.sword.ComponentIgnore.Value = true
} else {
s.sword.ComponentMovement.Speed = 1.0
s.sword.ComponentIgnore.Value = false
}
} else {
s.playerEntity.ComponentDash.Charge = 0
}
}