-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
326 lines (302 loc) · 7.33 KB
/
main.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
package main
import (
"embed"
"fmt"
"game/maps"
"game/role"
"game/tools"
_ "image/png"
"log"
"runtime"
"strconv"
"sync"
_ "net/http/pprof"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
//config
const (
IDLE int = 0
RUN int = 1
ATTACK int = 2
SCREENWIDTH int = 450
SCREENHEIGHT int = 300
OFFSETX int = 200
OFFSETY int = 80
PLAYERCENTERX int64 = 361
PLAYERCENTERY int64 = 219
SPEED float64 = 2
LAYOUTX int = 720
LAYOUTY int = 480
WEOFFSETX int = 127
WEOFFSETY int = 14
)
var game *Game
//Game
type Game struct {
count int
player *role.Player
maps *maps.MapBase
//monster *role.Monster
}
var (
counts int = 0
frameNums int = 4
flg bool = false
)
var op, opWea, opUI, opUI1, opSkill *ebiten.DrawImageOptions
var UI, UI1 *ebiten.Image
//go:embed resource
var images embed.FS
//factory
func NewGame() *Game {
//palayer
r := role.NewPlayer(float64(SCREENWIDTH/2), float64(SCREENHEIGHT/2), IDLE, 0, 0, 0, &images)
//map
m := maps.NewMap(&images)
gameStart := &Game{
count: 0,
player: r,
maps: m,
}
return gameStart
}
func init() {
w := sync.WaitGroup{}
w.Add(5)
//game init
game = NewGame()
//palyer init
go func() {
game.player.LoadImages()
w.Done()
}()
//map init
go func() {
game.maps.LoadMap()
w.Done()
}()
go func() {
//UI load
s, _ := images.ReadFile("resource/UI/liehuo.png")
mgUI := tools.GetEbitenImage(s)
UI = mgUI
w.Done()
}()
go func() {
//UI load
s, _ := images.ReadFile("resource/UI/chisha.png")
mgUI := tools.GetEbitenImage(s)
UI1 = mgUI
w.Done()
}()
go func() {
//UI
opUI = &ebiten.DrawImageOptions{}
opUI.Filter = ebiten.FilterLinear
opUI.GeoM.Translate(583, 380)
opUI1 = &ebiten.DrawImageOptions{}
opUI1.Filter = ebiten.FilterLinear
opUI1.GeoM.Translate(620, 330)
w.Done()
}()
w.Wait()
go func() {
runtime.GC()
}()
}
func (g *Game) Update() error {
g.count++
if g.player.State != ATTACK {
g.player.State = IDLE
}
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
x, y := ebiten.CursorPosition()
g.player.MouseX = x
g.player.MouseY = y
flg = true
}
//Calculate direction
dir := tools.CaluteDir(PLAYERCENTERX, PLAYERCENTERY, int64(g.player.MouseX), int64(g.player.MouseY))
//attack
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
//liehuo
if x > 583 && x < 627 && y > 380 && y < 424 {
g.player.SkillName = "liehuo"
if g.player.State != ATTACK {
counts = 0
}
flg = false
if g.player.Direction != dir || g.player.State != ATTACK {
g.player.SetPlayerState(ATTACK, dir)
}
}
if x > 621 && x < 664 && y > 331 && y < 373 {
g.player.SkillName = "chisha"
if g.player.State != ATTACK {
counts = 0
}
flg = false
if g.player.Direction != dir || g.player.State != ATTACK {
g.player.SetPlayerState(ATTACK, dir)
}
}
}
//keyboard controll
// if dir == 6 {
// if g.player.Direction != dir || g.player.State != RUN {
// g.player.SetPlayerState(RUN, dir)
// g.player.SetAnimator(1)
// }
// opBg.GeoM.Translate(SPEED, 0)
// g.player.X -= 2
// }
// if dir == 2 {
// if g.player.Direction != dir || g.player.State != RUN {
// g.player.SetPlayerState(RUN, dir)
// g.player.SetAnimator(1)
// }
// opBg.GeoM.Translate(-SPEED, 0)
// g.player.X += 2
// }
// if dir == 4 {
// if g.player.Direction != dir || g.player.State != RUN {
// g.player.SetPlayerState(RUN, dir)
// g.player.SetAnimator(1)
// }
// opBg.GeoM.Translate(0, -SPEED)
// g.player.Y += 2
// }
// if dir == 0 {
// if g.player.Direction != dir || g.player.State != RUN {
// g.player.SetPlayerState(RUN, dir)
// g.player.SetAnimator(1)
// }
// opBg.GeoM.Translate(0, SPEED)
// g.player.Y -= 2
// }
//mouse controll
if dir == 1 && flg {
if g.player.Direction != dir || g.player.State != RUN {
g.player.SetPlayerState(RUN, dir)
}
g.maps.OpBg.GeoM.Translate(-SPEED, SPEED)
g.player.Y -= 2
g.player.X += 2
flg = false
}
if dir == 3 && flg {
if g.player.Direction != dir || g.player.State != RUN {
g.player.SetPlayerState(RUN, dir)
}
g.maps.OpBg.GeoM.Translate(-SPEED, -SPEED)
g.player.Y += 2
g.player.X += 2
flg = false
}
if dir == 5 && flg {
if g.player.Direction != dir || g.player.State != RUN {
g.player.SetPlayerState(RUN, dir)
}
g.maps.OpBg.GeoM.Translate(SPEED, -SPEED)
g.player.Y += 2
g.player.X -= 2
flg = false
}
if dir == 7 && flg {
if g.player.Direction != dir || g.player.State != RUN {
g.player.SetPlayerState(RUN, dir)
}
g.maps.OpBg.GeoM.Translate(SPEED, SPEED)
g.player.Y -= 2
g.player.X -= 2
flg = false
}
//states
if g.player.State == IDLE {
frameNums = 6
g.player.SetPlayerState(IDLE, dir)
} else if g.player.State == ATTACK {
frameNums = 6
} else {
frameNums = 8
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
defer func() {
if r := recover(); r != nil {
fmt.Println("has error is :", r)
}
}()
//draw bg
screen.DrawImage(g.maps.BgImage, g.maps.OpBg)
//draw UI
screen.DrawImage(UI, opUI)
screen.DrawImage(UI1, opUI1)
//
name := ""
nameSkill := ""
switch g.player.State {
case ATTACK:
if counts >= 6 {
counts = 0
}
name = strconv.Itoa(g.player.Direction) + "_attack_" + strconv.Itoa(counts) + ".png"
nameSkill = strconv.Itoa(g.player.Direction) + "_skill_" + strconv.Itoa(counts) + ".png"
case IDLE:
if counts >= 6 {
counts = 0
}
name = strconv.Itoa(g.player.Direction) + "_stand_" + strconv.Itoa(counts) + ".png"
default:
name = strconv.Itoa(g.player.Direction) + "_run_" + strconv.Itoa(counts) + ".png"
}
imagess, x, y := g.player.GetAnimator("man", name)
//draw player
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(SCREENWIDTH/2+OFFSETX+x), float64(SCREENHEIGHT/2+OFFSETY+y))
op.GeoM.Scale(0.7, 0.7)
op.Filter = ebiten.FilterLinear
screen.DrawImage(imagess, op)
//draw wea
imagess, x, y = g.player.GetAnimator("weapon", name)
opWea = &ebiten.DrawImageOptions{}
opWea.GeoM.Translate(float64(SCREENWIDTH/2+WEOFFSETX+x), float64(SCREENHEIGHT/2+WEOFFSETY+y))
opWea.GeoM.Scale(0.7, 0.7)
opWea.Filter = ebiten.FilterLinear
screen.DrawImage(imagess, opWea)
//draw skill
if g.player.State == ATTACK {
imagey, x, y := g.player.GetAnimator("skill", nameSkill)
//skill option
opSkill = &ebiten.DrawImageOptions{}
opSkill.GeoM.Translate(float64(SCREENWIDTH/2+x), float64(SCREENHEIGHT/2+y))
opSkill.CompositeMode = ebiten.CompositeModeLighter
opSkill.GeoM.Scale(1.5, 1.5)
opSkill.Filter = ebiten.FilterLinear
screen.DrawImage(imagey, opSkill)
}
//draw debug
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS %d\nplayer position %d,%d\nmouse position %d,%d\ndir %d",
int64(ebiten.CurrentFPS()), int64(g.player.X), int64(g.player.Y), g.player.MouseX, g.player.MouseY, tools.CaluteDir(PLAYERCENTERX, PLAYERCENTERY, int64(g.player.MouseX), int64(g.player.MouseY))))
//change frame
if g.count > 5 {
counts++
g.count = 0
if counts >= frameNums {
counts = 0
}
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return LAYOUTX, LAYOUTY
}
func main() {
ebiten.SetWindowSize(SCREENWIDTH*2, SCREENHEIGHT*2)
ebiten.SetWindowTitle("golang game test")
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}