-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.go
151 lines (117 loc) · 2.82 KB
/
character.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
package main
import (
"image/color"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
)
var characterIsOutside = false
type character struct {
body *body
weapon *weapon
tick <-chan time.Time
}
func (c *character) Vel() pixel.Vec {
return c.body.vel
}
func (c *character) HandleCollision(x Collidable, collisionTime float64, normal pixel.Vec) {
switch collidable := x.(type) {
case *wall:
if normal.Y == 0 {
// collision in X. move back by c.body.vel (with a negated x)
c.body.rect = c.body.rect.Moved(c.body.vel.ScaledXY(pixel.V(-1, 0)))
} else {
// collision in Y. move back by c.body.vel (with a negated y)
c.body.rect = c.body.rect.Moved(c.body.vel.ScaledXY(pixel.V(0, -1)))
}
case *enemy:
// damage
select {
case <-c.tick:
if !collidable.isAttacking {
return
}
if playerScore.multiplier > 1 {
playerScore.setMultiplier(playerScore.multiplier - 1)
c.body.health -= 5
playerScore.incrementScore(-20.0)
if c.body.health <= 0 {
c.die()
}
} else {
c.body.health -= 20
playerScore.incrementScore(-20.0)
if c.body.health <= 0 {
c.die()
}
}
default:
}
}
}
func (c *character) Rect() pixel.Rect {
return c.body.rect
}
func (c *character) die() {
ded = true
isDodging = false
go playerScore.changeTrack(nightOnTheDocksAudio)
}
func (c *character) init() {
defer registerCollidable(c)
c.body = &body{
// phys
gravity: -512,
runSpeed: 300,
jumpSpeed: 192,
rect: pixel.R(-62, -74, 62, 74).Moved(playerSpawnPos),
rate: 1.0 / 10,
dir: -1,
}
c.body.init()
c.weapon = handgun
c.weapon.init()
c.tick = time.Tick(time.Millisecond * 200)
}
func (c *character) update(dt float64) {
c.body.update(dt)
c.weapon.update(dt, c.body.shootPos, c.body.vel)
characterIsOutside = c.body.rect.Norm().Intersect(streetBoundingRect.Norm()).Area() > 0
/*
r := c.body.rect.Norm()
fmt.Printf("min: %s, max: %s\n", r.Min, r.Max)
*/
}
func (c *character) draw(t pixel.Target) {
c.body.draw(t)
c.weapon.draw(t)
}
type hat struct {
pos pixel.Vec
counter float64
color, altColor color.Color
}
func (h *hat) init() {
}
// @TODO hat should move up and down with animation
func (h *hat) update(dt float64, target pixel.Vec) {
h.pos.X = target.X
h.pos.Y = target.Y
h.pos.Y += 6
}
func (h *hat) draw(t pixel.Target) {
imd := imdraw.New(nil)
imd.Color = h.color
imd.Push(pixel.V(h.pos.X, h.pos.Y))
imd.Push(pixel.V(h.pos.X-5, h.pos.Y+0))
imd.Push(pixel.V(h.pos.X-5, h.pos.Y+1))
imd.Push(pixel.V(h.pos.X-2.5, h.pos.Y+1))
imd.Color = h.altColor
imd.Push(pixel.V(h.pos.X-2.5, h.pos.Y+5))
imd.Push(pixel.V(h.pos.X+2.5, h.pos.Y+5))
imd.Push(pixel.V(h.pos.X+2.5, h.pos.Y+1))
imd.Push(pixel.V(h.pos.X+5, h.pos.Y+1))
imd.Push(pixel.V(h.pos.X+5, h.pos.Y+0))
imd.Polygon(0)
imd.Draw(t)
}