forked from gophergala2016/gophette
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcharacter.go
199 lines (180 loc) · 5.17 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
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
package main
type CharacterParams struct {
AccelerationX int
DecelerationX int
MaxSpeedX int
MaxSpeedY int
InitialJumpSpeedY int
HighGravity int
LowGravity int
RunFrameDelay int
}
var HeroParams = CharacterParams{
AccelerationX: 2,
DecelerationX: 1,
MaxSpeedX: 10,
MaxSpeedY: 32,
InitialJumpSpeedY: -23,
HighGravity: 2,
LowGravity: 1,
RunFrameDelay: 3,
}
var BarneyParams = CharacterParams{
AccelerationX: 2,
DecelerationX: 1,
MaxSpeedX: 11,
MaxSpeedY: 32,
InitialJumpSpeedY: -25,
HighGravity: 2,
LowGravity: 1,
RunFrameDelay: 5,
}
type Character struct {
Direction int
Position Rectangle
SpeedX int
SpeedY int
InAir bool
Params CharacterParams
collisionRect Rectangle
runFrames [DirectionCount][]Image
standFrames [DirectionCount]Image
jumpFrames [DirectionCount]Image
runFrameIndex int
nextRunFrame int
}
func NewHero(assets AssetLoader) *Character {
return &Character{
Position: assets.LoadRectangle("hero collision"),
collisionRect: assets.LoadRectangle("hero collision"),
Params: HeroParams,
runFrames: [DirectionCount][]Image{
[]Image{
assets.LoadImage("gophette_left_run1"),
assets.LoadImage("gophette_left_run2"),
assets.LoadImage("gophette_left_run1"),
assets.LoadImage("gophette_left_run3"),
},
[]Image{
assets.LoadImage("gophette_right_run1"),
assets.LoadImage("gophette_right_run2"),
assets.LoadImage("gophette_right_run1"),
assets.LoadImage("gophette_right_run3"),
},
},
standFrames: [DirectionCount]Image{
assets.LoadImage("gophette_left_run1"),
assets.LoadImage("gophette_right_run1"),
},
jumpFrames: [DirectionCount]Image{
assets.LoadImage("gophette_left_jump"),
assets.LoadImage("gophette_right_jump"),
},
}
}
func NewBarney(assets AssetLoader) *Character {
return &Character{
Position: assets.LoadRectangle("barney collision"),
collisionRect: assets.LoadRectangle("barney collision"),
Params: BarneyParams,
runFrames: [DirectionCount][]Image{
[]Image{
assets.LoadImage("barney_left_run1"),
assets.LoadImage("barney_left_run2"),
assets.LoadImage("barney_left_run3"),
assets.LoadImage("barney_left_run4"),
assets.LoadImage("barney_left_run5"),
assets.LoadImage("barney_left_run6"),
},
[]Image{
assets.LoadImage("barney_right_run1"),
assets.LoadImage("barney_right_run2"),
assets.LoadImage("barney_right_run3"),
assets.LoadImage("barney_right_run4"),
assets.LoadImage("barney_right_run5"),
assets.LoadImage("barney_right_run6"),
},
},
standFrames: [DirectionCount]Image{
assets.LoadImage("barney_left_stand"),
assets.LoadImage("barney_right_stand"),
},
jumpFrames: [DirectionCount]Image{
assets.LoadImage("barney_left_jump"),
assets.LoadImage("barney_right_jump"),
},
}
}
func (c *Character) Reset(dir int) {
c.Direction = dir
c.SpeedX = 0
c.SpeedY = 0
c.InAir = false
}
func (c *Character) SetBottomCenterTo(x, y int) {
c.Position.X = x - c.Position.W/2
c.Position.Y = y - c.Position.H
}
func (c *Character) Render() {
var frame Image
if c.InAir {
frame = c.jumpFrames[c.Direction]
} else if c.SpeedX == 0 {
frame = c.standFrames[c.Direction]
} else {
frame = c.runFrames[c.Direction][c.runFrameIndex]
}
// the position is that of the collision rectangle, the image does not have
// the same size as the collision rectangle so it must be offset relative
// to the collision rectangle's top-left corner for drawing
frame.DrawAt(
c.Position.X-c.collisionRect.X,
c.Position.Y-c.collisionRect.Y,
)
}
type Collider interface {
MoveInX(bounds Rectangle, dx int) (newBounds Rectangle, collided bool)
MoveInY(bounds Rectangle, dy int) (newBounds Rectangle, collided bool)
}
func (c *Character) Update(collider Collider) {
// face the way of the horizonal speed
if c.SpeedX < 0 {
c.Direction = LeftDirectionIndex
}
if c.SpeedX > 0 {
c.Direction = RightDirectionIndex
}
// NOTE putting this code AFTER the collision detection will not have the
// character run while stuck in a wall; BEFORE the collision detection will
// keep the running animation while hitting the wall
if c.SpeedX == 0 {
c.runFrameIndex = 0
c.nextRunFrame = 0
} else {
c.nextRunFrame--
if c.nextRunFrame <= 0 {
c.runFrameIndex = (c.runFrameIndex + 1) % len(c.runFrames[c.Direction])
c.nextRunFrame = c.Params.RunFrameDelay
}
}
// Move in Y first, this assures that you land on a platform even if it is
// at the maximum jump height; in this case you move up above the platform,
// then you move in X in the next step and land on the platform.
// Were it the other way round would mean moving in X, hitting the platform,
// then moving in Y above the platform but to the side of it
var collided bool
c.InAir = true // assume this until proven otherwise
c.Position, collided = collider.MoveInY(c.Position, c.SpeedY)
if collided {
if c.SpeedY > 0 {
// if she was going down, she now landed on the ground
c.InAir = false
}
c.SpeedY = 0
}
// move in X
c.Position, collided = collider.MoveInX(c.Position, c.SpeedX)
if collided {
c.SpeedX = 0
}
}