-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.go
More file actions
341 lines (283 loc) · 7.57 KB
/
Copy pathplayer.go
File metadata and controls
341 lines (283 loc) · 7.57 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/setanarut/aseplayer"
"github.com/setanarut/coll"
"github.com/setanarut/kamera/v2"
"github.com/setanarut/tween"
"github.com/setanarut/v"
)
const (
DashWallBlockDistance = 34
)
const (
MoveSpeedX = 6.125
MoveSpeedXAir = 5.5
MaxSpeedY = 20.25
JumpPower = -11.46
ParryPower = -14.46
Gravity = 0.86
DashSpeed = 13.75 * 1.5
BulletSpeed = 13.0
BulletRadius = 3.0
)
// Hitbox dimensions constants
const (
DuckHalfY = 12.0 // 24
FallHalfY = 16.0 // 32
// 32x70
HalfX = 17
HalfY = 35
DuckTweenLerpSpeed = 0.30
)
// Durations
const (
JumpHoldMinDuration = time.Millisecond
JumpHoldMaxDuration = time.Millisecond * 160
BulletDuration = time.Millisecond * 250
DashCooldown = time.Millisecond * 300
ParryDuration = time.Millisecond * 200
DashTimeDuration = time.Millisecond * 300
HitDuration = time.Millisecond * 2000
HitBlinkInterval = HitDuration / 4
Tick = time.Second / 60
)
var bulletOffsetToggler OffsetToggler = 8.0
// Player yapısı
type Player struct {
coll.AABB
OldAABB coll.AABB
ParrySensor coll.Circle
Hp int
MoveDirectionX float64
Facing float64
FacingOld float64
Direction8 v.Vec
Delta v.Vec
activeState State
previousState State
fall *fall
grounded *grounded
jumping *jump
dash *dash
hit *hit
JumpTimer time.Duration
DashTimer time.Duration
BulletTimer time.Duration
TimeSinceGroundDash time.Duration
ParryTimer time.Duration
FireTimer time.Duration
HitTimer time.Duration
IsParrying bool
ParryReady bool
DashUsedInAir bool
DashEasingDisabled bool
IsOnFloor bool
Paused bool
JumpPressed bool
firePressed bool
fireJustPressed bool
duckPressed bool
dashPressed bool
lockPressed bool
jumpJustPressed bool
dashJustPressed bool
hitInfo coll.Hit
groundedPlatform any
oldGroundedPlatform any
dio *ebiten.DrawImageOptions
animPlayer *aseplayer.AnimPlayer
currentLevel ILevel
}
func NewPlayer(pos v.Vec) *Player {
p := &Player{
AABB: coll.AABB{
Pos: pos,
Half: v.Vec{HalfX, HalfY},
},
ParrySensor: coll.Circle{
Pos: pos,
Radius: 43,
},
Facing: 1, // 0 ile başlamasın diye
Direction8: v.Right, // 0 ile başlamasın diye
TimeSinceGroundDash: time.Second * 7,
hitInfo: coll.Hit{},
fall: &fall{},
jumping: &jump{},
dash: &dash{},
hit: &hit{},
grounded: &grounded{
activeSubState: nil,
idle: &idle{},
run: &run{},
runTurn: &runTurn{},
duck: &duck{},
duckIdle: &duckIdle{},
duckTurn: &duckTurn{},
lock: &lock{},
},
dio: &ebiten.DrawImageOptions{
Filter: ebiten.FilterLinear,
},
}
p.dash.dashTween = *tween.NewTween(0, DashSpeed, DashTimeDuration, tween.InOutSine, false)
p.animPlayer = readAseprite("cuphead.ase")
p.animPlayer.Play("jump")
p.activeState = p.fall
p.grounded.activeSubState = p.grounded.idle
return p
}
func (p *Player) ChangeState(next State) {
if p.activeState == next {
return
}
p.previousState = p.activeState
p.activeState.Exit(p)
p.activeState = next
p.activeState.Enter(p)
}
func (p *Player) IsState(s State) bool {
return p.activeState == s
}
func (p *Player) IsSubState(s State) bool {
return p.grounded.activeSubState == s
}
func (p *Player) fireDuck() {
if p.BulletTimer > 0 {
return
}
p.BulletTimer = BulletDuration
bulletPos := v.Vec{p.Pos.X + (50 * p.Facing), p.Pos.Y - 3}
bulletPos.Y += bulletOffsetToggler.Next()
ShootManag.SpawnBullet(bulletPos, v.Vec{p.Facing * BulletSpeed, 0})
}
func (p *Player) fire8Direction() {
if p.BulletTimer > 0 {
return
}
p.BulletTimer = BulletDuration // Timer'ı SADECE burada sıfırla
var bulletPos v.Vec
if IsAxisHorizontal(p.Direction8) {
bulletPos.X = p.Pos.X + (50 * p.Facing)
bulletPos.Y = p.Pos.Y - bulletOffsetToggler.Next()
} else if IsAxisDiagonal(p.Direction8) {
bulletPos.X = p.Pos.X + (45 * p.Facing)
bulletPos.Y = p.Pos.Y + (30 * p.Direction8.Y)
} else if IsVertical(p.Direction8) {
bulletPos = p.Pos.Add(p.Direction8.Unit().Scale(60))
bulletPos.X += p.Facing * 15
bulletPos.Y += bulletOffsetToggler.Next()
}
bulletVelocity := p.Direction8.Unit().Scale(BulletSpeed)
if p.Delta.X != 0 {
bulletVelocity.X += p.Delta.X
}
ShootManag.SpawnBullet(bulletPos, bulletVelocity)
}
func (p *Player) IsOnPlatform() bool {
return p.groundedPlatform != nil
}
func (p *Player) StopDashing() {
p.TimeSinceGroundDash = 0
p.DashTimer = DashTimeDuration
}
func (p *Player) inputUpdate() {
p.Direction8 = Axis()
p.MoveDirectionX = getAxisX()
p.jumpJustPressed = inpututil.IsKeyJustPressed(ebiten.KeySpace)
p.dashJustPressed = inpututil.IsKeyJustPressed(ebiten.KeyUp)
p.fireJustPressed = inpututil.IsKeyJustPressed(ebiten.KeyLeft)
p.JumpPressed = ebiten.IsKeyPressed(ebiten.KeySpace)
p.dashPressed = ebiten.IsKeyPressed(ebiten.KeyUp)
p.lockPressed = ebiten.IsKeyPressed(ebiten.KeyDown)
p.firePressed = ebiten.IsKeyPressed(ebiten.KeyLeft)
p.duckPressed = ebiten.IsKeyPressed(ebiten.KeyS)
}
func (p *Player) Teleport(pos v.Vec) {
p.Pos = pos
p.OldAABB = p.AABB
p.Delta = v.Vec{}
p.IsOnFloor = false
p.ChangeState(p.fall)
}
func (s *Player) position() v.Vec {
return s.AABB.Pos
}
func (p *Player) IsHitTimerEnd() bool {
return p.HitTimer <= 0
}
func (p *Player) Update() {
if p.Paused {
return
}
if p.BulletTimer > 0 {
p.BulletTimer -= Tick
}
if p.HitTimer > 0 {
p.HitTimer -= Tick
}
p.FacingOld = p.Facing
p.OldAABB = p.AABB
p.inputUpdate()
if p.MoveDirectionX != 0 && p.activeState != p.dash {
p.Facing = p.MoveDirectionX
}
if p.Direction8.IsZero() {
p.Direction8.X = p.Facing
}
// Zemin dash cooldown timer'ını güncelle
p.TimeSinceGroundDash = min(p.TimeSinceGroundDash+Tick, DashCooldown)
// ############ FSM Durumlarını güncelle ###################
p.activeState.Update(p)
p.animPlayer.Update(aseplayer.Delta)
// Yerçekimi
if p.activeState != p.dash {
p.Delta.Y += Gravity
}
p.Delta.Y = min(p.Delta.Y, MaxSpeedY) // Speed limit
// ############ OYUNCU HIZINI EKLE ###################
p.Pos = p.Pos.Add(p.Delta)
if p.IsOnFloor && p.IsOnPlatform() {
if shape, ok := p.groundedPlatform.(*BoxShape); ok {
if shape.OneWay {
if p.duckPressed && !p.lockPressed && p.jumpJustPressed {
p.oldGroundedPlatform = p.groundedPlatform
shape.Solid = false
}
}
if !shape.parent.StaticBody {
platformDeltaX := shape.Pos.X - shape.OldPos.X
p.Pos.X += platformDeltaX
// if !dp.Paused {
p.Delta.Y = shape.Pos.Y - shape.OldPos.Y // platformun deltasını oyuncu ile eşitle
// }
p.SetBottom(shape.Top())
}
}
}
p.ParrySensor.Pos = p.Pos
p.groundedPlatform = nil // reset
p.IsOnFloor = false
}
func (p *Player) Draw(s *ebiten.Image, cam *kamera.Camera) {
p.dio.GeoM.Reset()
p.dio.GeoM.Translate(p.animPlayer.CurrentFrame.Position.X, p.animPlayer.CurrentFrame.Position.Y)
p.dio.GeoM.Scale(SpriteScale*p.Facing, SpriteScale)
switch p.activeState {
case p.grounded:
p.dio.GeoM.Translate(p.Pos.X, p.Bottom())
default:
p.dio.GeoM.Translate(p.Pos.X, p.Pos.Y)
}
p.dio.ColorScale.Reset()
if p.HitTimer > 0 {
// Her 500ms'de bir yanıp sön (4 periyot toplam)
if (p.HitTimer/HitBlinkInterval)%2 == 0 {
p.dio.ColorScale.ScaleAlpha(0.3)
}
}
cam.Draw(p.animPlayer.CurrentFrame.Image, p.dio, s)
}