-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliving_entity.go
361 lines (285 loc) · 8.46 KB
/
living_entity.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"fmt"
"image"
"image/color"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
type ILivingEntity interface {
SetGame(*Game)
StartWalk(LookDirection)
EndWalk()
GetTilePos() (int, error)
Update()
Draw(*ebiten.Image)
DrawDebugInfo(*ebiten.Image)
GetHealth() float64
GetWorldPos() Vec2f
GetLivingEntity() *LivingEntity
ProcessWalk()
}
type LivingEntity struct {
etype interface{}
entityClass string
look LookDirection
walking bool
worldPos Vec2f
anchorPos Vec2f
baseSpeed float64
health float64
speedModifier float64
sprite *ebiten.Image
game *Game
id int
spells []ISpell
prevTilePos int
}
func (e *LivingEntity) SetGame(g *Game) {
e.game = g
}
func (e *LivingEntity) StartWalk(look LookDirection) {
e.walking = true
e.look = look
}
func (e *LivingEntity) EndWalk() {
e.walking = false
}
func (e *LivingEntity) ProcessWalk() {
if e.walking {
speed := e.baseSpeed * e.speedModifier
switch look := e.look; look {
case LooksRight:
if !e.game.IsTileSolidAt(e.worldPos.X+speed, e.worldPos.Y) {
e.worldPos.X += speed
}
case LooksLeft:
if !e.game.IsTileSolidAt(e.worldPos.X-speed, e.worldPos.Y) {
e.worldPos.X -= speed
}
case LooksUp:
if !e.game.IsTileSolidAt(e.worldPos.X, e.worldPos.Y-speed) {
e.worldPos.Y -= speed
}
case LooksDown:
if !e.game.IsTileSolidAt(e.worldPos.X, e.worldPos.Y+speed) {
e.worldPos.Y += speed
}
}
}
}
func (e *LivingEntity) GetTilePos() (int, error) {
return e.game.WorldPosToTilePos(e.worldPos.X, e.worldPos.Y)
}
func (e *LivingEntity) Update() {
for _, spell := range e.spells {
spell.Update()
}
}
func (e *LivingEntity) SetSpeedModifier(speed float64) {
e.speedModifier = speed
}
const CHAR_TILE_SIZE int = 16
func (e *LivingEntity) DrawHealthBar(screen *ebiten.Image) {
cameraZoom := e.game.camera.GetZoom()
// screenPos := e.game.camera.WorldToScreen(e.worldPos)
borderColor := color.RGBA{0, 0, 0, 255}
fillColor := HSL{math.Pow(e.health/100.0, 2) * 0.33, 1.0, 0.5}.ToRGB()
// health bar offset from character origin in world coordinates
healthBarOffset := Vec2f{-(float64(CHAR_TILE_SIZE) / 2), -(float64(CHAR_TILE_SIZE) + 4)}
pos := Vec2f{screenWidth / 2, screenHeight / 2}
pos = pos.Translate(
e.game.camera.GetCurrentPosition().Translate(
e.GetWorldPos().Translate(healthBarOffset).Negate()).Scale(
e.game.camera.GetZoom()).Negate())
ebitenutil.DrawRect(
screen,
pos.X,
pos.Y,
16*cameraZoom,
3*cameraZoom, borderColor)
ebitenutil.DrawRect(
screen,
pos.X+cameraZoom,
pos.Y+cameraZoom,
(e.health/100.0)*(16-2)*cameraZoom, cameraZoom, fillColor)
pos = pos.Translate(Vec2f{0, -11})
fontRenderer := e.game.fontRenderer
fontRenderer.PushState()
fontRenderer.Reset()
fontRenderer.SetTextColor(color.White)
if e.game.camera.targetEntity.GetLivingEntity() == e {
fontRenderer.SetTextColor(color.RGBA{255, 0, 0, 255})
}
fontRenderer.DrawTextAt(screen, e.entityClass, pos)
fontRenderer.PopState()
}
type ISpell interface {
Update()
Draw(*ebiten.Image)
GetSpell() *Spell
}
type Spell struct {
creationTime int
caster ILivingEntity
}
func (s *Spell) GetSpell() *Spell {
return s
}
type MonobearExplosion struct {
Spell
}
func (s *MonobearExplosion) Update() {
/*
elapsed := tickCounter - s.creationTime
if elapsed >= 256 {
for i, spell := range s.caster.GetLivingEntity().spells {
if s.GetSpell() == spell.GetSpell() {
s.caster.GetLivingEntity().spells[i] = nil
s.caster.GetLivingEntity().spells = append(s.caster.GetLivingEntity().spells[:i], s.caster.GetLivingEntity().spells[i+1:]...)
break
}
}
}
*/
}
func CreateMonobearExplosion(caster ILivingEntity) *MonobearExplosion {
s := new(MonobearExplosion)
s.caster = caster
s.creationTime = tickCounter
return s
}
func (s *MonobearExplosion) Draw(screen *ebiten.Image) {
e := s.caster.GetLivingEntity()
{
op := &ebiten.DrawImageOptions{}
pos := e.game.camera.WorldToScreen2(e.worldPos)
op.GeoM.Translate(-16, -16)
op.GeoM.Translate(pos.X, pos.Y)
screen.DrawImage(skillMonobearExplosion, op)
}
{
screenCenter := Vec2f{screenWidth / 2, screenHeight / 2}
cameraWorldPos := e.game.camera.GetCurrentPosition()
entityWorldPos := e.GetWorldPos()
cameraZoom := e.game.camera.GetZoom()
var pos Vec2f
pos = cameraWorldPos.Subtract(entityWorldPos)
pos = pos.Scale(cameraZoom)
pos = screenCenter.Subtract(pos)
e.game.fontRenderer.DrawTextAt(screen, "Monobear Explosion", pos)
}
{
op := &ebiten.DrawImageOptions{}
screenCenter := Vec2f{screenWidth / 2, screenHeight / 2}
cameraWorldPos := e.game.camera.GetCurrentPosition()
entityWorldPos := e.GetWorldPos()
cameraZoom := e.game.camera.GetZoom()
var pos Vec2f
pos = cameraWorldPos.Subtract(entityWorldPos)
pos = pos.Scale(cameraZoom)
pos = screenCenter.Subtract(pos)
i := tickCounter / 4 % 12
sx := (i % 4) * 16
sy := (i / 4) * 16
op.GeoM.Translate(-(float64(tileSize) / 2), -(float64(tileSize) / 2))
op.GeoM.Scale(cameraZoom, cameraZoom)
op.GeoM.Translate(pos.X, pos.Y)
tile := explosionSprite.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image)
screen.DrawImage(tile, op)
}
}
func (e *LivingEntity) Draw(screen *ebiten.Image) {
var walkAnim int
var spriteLine int
var sx int
var sy int
var sprite *ebiten.Image
op := &ebiten.DrawImageOptions{}
walkAnim = 0
if e.walking {
speed := e.baseSpeed * e.speedModifier
animTicker := int(float64(float64(tickCounter) * speed))
walkAnim += animTicker / 4 % 3
}
spriteLine = int(e.look) * 4
sx = ((spriteLine + walkAnim) % 4) * tileSize
sy = ((spriteLine + walkAnim) / 4) * tileSize
cameraZoom := e.game.camera.GetZoom()
pos := e.game.camera.WorldToScreen2(e.worldPos)
op.GeoM.Translate(-e.anchorPos.X, -e.anchorPos.Y)
op.GeoM.Scale(cameraZoom, cameraZoom)
op.GeoM.Translate(pos.X, pos.Y)
sprite = e.sprite.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image)
screen.DrawImage(sprite, op)
e.DrawHealthBar(screen)
}
func (e *LivingEntity) DrawDebugInfo(screen *ebiten.Image) {
// health bar offset from character origin in world coordinates
healthBarOffset := Vec2f{float64(CHAR_TILE_SIZE) / 2, -(float64(CHAR_TILE_SIZE))}
pos := Vec2f{screenWidth / 2, screenHeight / 2}
pos = pos.Translate(
e.game.camera.GetCurrentPosition().Translate(
e.GetWorldPos().Translate(healthBarOffset).Negate()).Scale(
e.game.camera.GetZoom()).Negate())
infoPrinters := []func(*LivingEntity) string{
func(e *LivingEntity) string {
return fmt.Sprintf("x: %f, y: %f", e.GetWorldPos().X, e.GetWorldPos().Y)
},
func(e *LivingEntity) string {
return fmt.Sprintf("walking: %t", e.walking)
},
func(e *LivingEntity) string {
return fmt.Sprintf("health: %f", e.health)
},
func(e *LivingEntity) string {
tilePos, _ := e.GetTilePos()
return fmt.Sprintf("CurTilePos: %d", tilePos)
},
func(e *LivingEntity) string {
return fmt.Sprintf("PrevTilePos: %d", e.GetLivingEntity().prevTilePos)
},
}
fontRenderer := e.game.fontRenderer
fontRenderer.PushState()
fontRenderer.Reset()
for i, printer := range infoPrinters {
fontRenderer.DrawTextAt(screen, printer(e), pos)
pos = pos.Translate(Vec2f{0, float64(fontRenderer.GetGlyphSize().X+1) * float64(i)})
}
fontRenderer.PopState()
}
var eidCounter int
func (e *LivingEntity) _ConstructLivingEntity(g *Game) {
e.worldPos = Vec2f{CHARACTER_SPAWN_X, CHARACTER_SPAWN_Y}
e.anchorPos = Vec2f{CHARACTER_ANCHOR_X, CHARACTER_ANCHOR_Y}
e.baseSpeed = 1.0
e.speedModifier = 1.0
e.health = 100.0
e.id = eidCounter
eidCounter++
e.SetGame(g)
}
func (e *LivingEntity) GetUnderlyingTiles() ([]Tile, error) {
var tiles []Tile
tilePos, err := e.GetTilePos()
if err != nil {
return nil, err
}
for _, layer := range e.game.level.tileLayers {
if layer[tilePos] != Tile(tileIDEmpty) {
tiles = append(tiles, layer[tilePos])
}
}
return tiles, err
}
func (e *LivingEntity) GetHealth() float64 {
return e.health
}
func (e *LivingEntity) GetWorldPos() Vec2f {
return e.worldPos
}
func (e *LivingEntity) GetLivingEntity() *LivingEntity {
return e
}