generated from sionleroux/ebitengine-game-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.go
221 lines (203 loc) · 4.64 KB
/
creep.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
// Copyright 2022 Siôn le Roux. All rights reserved.
// Use of this source code is subject to an MIT-style
// licence which can be found in the LICENSE file.
package main
import (
"errors"
"image"
"log"
"github.com/hajimehoshi/ebiten/v2"
)
// Creep moves along a path from a spawn point towards the base it is attacking
type Creep struct {
Coords image.Point
NextWaypoint int
Health int // Hit points
Damage int // How much damage it deals to the base
Loot int // How much money you get when it dies
Frame int
LastMoved int
Direction int // Which way the creep is moving
Flip bool // Whether to flip the animation frame
Sprite *SpriteSheet
}
// NewTinyCreep returns a new creep with properties copied from creepTiny
func NewTinyCreep(g *Game) *Creep {
return &Creep{
NextWaypoint: 1,
Health: 200,
Loot: 30,
Sprite: g.Sprites[spriteTinyMonster],
}
}
// NewSmallCreep returns a new creep with properties copied from creepSmall
func NewSmallCreep(g *Game) *Creep {
return &Creep{
NextWaypoint: 1,
Health: 1000,
Loot: 50,
Sprite: g.Sprites[spriteSmallMonster],
}
}
// NewBigCreep returns a new creep with properties copied from creepBig
func NewBigCreep(g *Game) *Creep {
return &Creep{
NextWaypoint: 1,
Health: 4500,
Loot: 200,
Sprite: g.Sprites[spriteBigMonsterVertical],
}
}
// NewWaves makes new waves of creeps
func NewWaves(g *Game) []Creeps {
return []Creeps{
{
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewBigCreep(g),
},
{
NewTinyCreep(g),
NewTinyCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewTinyCreep(g),
NewTinyCreep(g),
NewSmallCreep(g),
NewBigCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewTinyCreep(g),
NewTinyCreep(g),
NewSmallCreep(g),
NewBigCreep(g),
NewSmallCreep(g),
NewSmallCreep(g),
NewBigCreep(g),
},
}
}
const (
directionRight int = iota
directionLeft
directionUp
directionDown
)
// Update handles game logic for a Creep
func (c *Creep) Update(g *Game) error {
if c.Health <= 0 {
g.Money += c.Loot
return errors.New("Creep died")
}
c.LastMoved = (c.LastMoved + 1) % 10
if c.LastMoved != 0 {
return nil
}
c.navigateWaypoints(g)
c.animate()
return nil
}
func (c *Creep) animate() {
const (
HORIZONTAL = 0
VERTICAL = 2
)
var frameTag int
switch c.Direction {
case directionRight:
c.Flip = false
frameTag = HORIZONTAL
case directionLeft:
c.Flip = true
frameTag = HORIZONTAL
default:
c.Flip = false
frameTag = VERTICAL
}
from := c.Sprite.Meta.FrameTags[frameTag].From
to := c.Sprite.Meta.FrameTags[frameTag].To
if c.Frame < from || c.Frame >= to {
c.Frame = from
return
}
if c.Frame < to {
c.Frame++
}
}
func (c *Creep) navigateWaypoints(g *Game) {
tileSize := 7
hudOffset := 5
tileCenter := 4
targetSquare := g.MapData[c.NextWaypoint]
targertCoords := image.Pt(
targetSquare.X*tileSize+tileCenter,
targetSquare.Y*tileSize+tileCenter+hudOffset,
)
if targertCoords.X > c.Coords.X {
c.Coords.X++
c.Direction = directionRight
}
if targertCoords.X < c.Coords.X {
c.Coords.X--
c.Direction = directionLeft
}
if targertCoords.Y > c.Coords.Y {
c.Coords.Y++
c.Direction = directionUp
}
if targertCoords.Y < c.Coords.Y {
c.Coords.Y--
c.Direction = directionDown
}
if targertCoords.X == c.Coords.X && targertCoords.Y == c.Coords.Y {
next := c.NextWaypoint + 1
if next < len(g.MapData) {
c.NextWaypoint++
} else {
log.Println("You failed")
g.State = gameStateLose
}
}
}
// Attack hurts a creep's health by a specified amount
func (c *Creep) Attack(amount int) bool {
c.Health = c.Health - amount
if c.Health <= 0 {
return true
}
return false
}
// Draw draws the Creep to the screen
func (c *Creep) Draw(g *Game, screen *ebiten.Image) {
s := c.Sprite
frame := s.Sprite[c.Frame]
op := &ebiten.DrawImageOptions{}
if c.Flip { // Please don't ask
op.GeoM.Translate(float64(-1*frame.Position.W/2), 1)
op.GeoM.Scale(-1, 1)
op.GeoM.Translate(float64(frame.Position.W/2), 1)
}
op.GeoM.Translate(float64(c.Coords.X-3), float64(c.Coords.Y-3))
screen.DrawImage(s.Image.SubImage(image.Rect(
frame.Position.X,
frame.Position.Y,
frame.Position.X+frame.Position.W,
frame.Position.Y+frame.Position.H,
)).(*ebiten.Image), op)
}
// Creeps is a slice of Creep entities
type Creeps []*Creep