-
Notifications
You must be signed in to change notification settings - Fork 0
/
gun.go
193 lines (172 loc) · 4.04 KB
/
gun.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"errors"
"sync"
"sync/atomic"
"time"
)
var ReloadError = errors.New("gun on reload")
var GunConfigError = errors.New("gun not configurated")
var OutAmmoError = errors.New("out of ammo")
type FireParams struct {
Position, Direction, BaseSpeed Point
Owner ObjectInterface
}
type GunState struct {
Projectile string
Name string
Ammo int64
ShotQueue int
PerShotQueueTime time.Duration
ReloadTime time.Duration
lastShotTime time.Time
}
type Gun struct {
Owner *Unit
Current *GunState
State []*GunState
mutex sync.Mutex
}
func (receiver *Gun) Fire() error {
receiver.mutex.Lock()
var current = receiver.Current
if current == nil {
receiver.mutex.Unlock()
return GunConfigError
}
var delayAccumulator time.Duration
if receiver.IsReloading() {
receiver.mutex.Unlock()
return ReloadError
}
current.lastShotTime = time.Now()
receiver.mutex.Unlock()
params := receiver.getParams()
for i := 0; i < current.ShotQueue; i++ {
if current.Ammo != -1 && current.Ammo <= 0 {
return OutAmmoError
}
if current.PerShotQueueTime > 0 && i > 1 {
delayAccumulator += current.PerShotQueueTime
time.AfterFunc(delayAccumulator, func() {
if receiver.Owner.destroyed {
return
}
receiver.Owner.Trigger(FireEvent, receiver.Owner, params)
current.lastShotTime = time.Now()
if current.Ammo > 0 {
current.Ammo--
}
})
} else {
receiver.Owner.Trigger(FireEvent, receiver.Owner, params)
current.lastShotTime = time.Now()
if current.Ammo > 0 {
current.Ammo--
}
}
}
return nil
}
func (receiver *Gun) IsReloading() bool {
if receiver.Current.lastShotTime.IsZero() {
return false
}
if time.Now().Sub(receiver.Current.lastShotTime) > receiver.Current.ReloadTime {
return false
}
return true
}
func (receiver *Gun) GetProjectile() string {
if receiver.Current == nil {
return ""
}
return receiver.Current.Projectile
}
func (receiver *Gun) GetName() string {
if receiver.Current.Name != "" {
return receiver.Current.Name
}
return "N/A"
}
func (receiver *Gun) IncAmmoIfAcceptable(byValue int64) int64 {
for {
//what if receiver.Current change???
if ref := atomic.LoadInt64(&receiver.Current.Ammo); ref > -1 {
if !atomic.CompareAndSwapInt64(&receiver.Current.Ammo, ref, ref+byValue) {
continue
} else {
return atomic.LoadInt64(&receiver.Current.Ammo)
}
} else {
return -1
}
}
}
func (receiver *Gun) Reset() {
receiver.mutex.Lock()
defer receiver.mutex.Unlock()
receiver.Current = receiver.State[0]
}
func (receiver *Gun) Downgrade() {
receiver.mutex.Lock()
defer receiver.mutex.Unlock()
receiver.Current = receiver.State[0]
}
func (receiver *Gun) Upgrade(state *GunState) {
receiver.mutex.Lock()
defer receiver.mutex.Unlock()
receiver.State[1] = state
receiver.Current = state
}
func (receiver *Gun) Basic(state *GunState) {
receiver.mutex.Lock()
defer receiver.mutex.Unlock()
receiver.State[0] = state
receiver.Current = state
}
func (receiver *Gun) getPosition() Point {
point := receiver.Owner.GetXY()
dir := receiver.Owner.Direction
size := receiver.Owner.GetWH()
if dir.X == 0 && dir.Y == 0 {
dir.Y = -1
}
centerX := point.X + size.W/2
centerY := point.Y + size.H/2
return Point{
X: centerX + (dir.X * size.W / 2),
Y: centerY + (dir.Y * size.H / 2),
}
}
func (receiver *Gun) getParams() FireParams {
return FireParams{
Position: receiver.getPosition(),
BaseSpeed: receiver.Owner.Speed,
Direction: receiver.Owner.Direction,
Owner: receiver.Owner,
}
}
func (receiver *Gun) Copy() *Gun {
instance := *receiver
instance.State = make([]*GunState, len(receiver.State), cap(receiver.State))
for i, state := range receiver.State {
if state == nil {
continue
}
copy := *state
instance.State[i] = ©
if state == receiver.Current {
instance.Current = ©
}
}
instance.mutex = sync.Mutex{}
return &instance
}
func NewGun(owner *Unit) (*Gun, error) {
return &Gun{
Owner: owner,
Current: nil,
State: make([]*GunState, 2, 2),
}, nil
}