-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
149 lines (130 loc) · 3.07 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
package main
import (
"korok.io/korok/game"
"korok.io/korok"
"korok.io/korok/asset"
"korok.io/korok/math/f32"
"korok.io/korok/gfx"
"korok.io/korok/gui"
"korok.io/korok/engi"
"korok.io/korok/anim"
"korok.io/korok/math/ease"
"korok.io/korok/gfx/font"
)
type StartScene struct {
title struct{
gfx.Tex2D
gui.Rect
}
start struct{
btnNormal gfx.Tex2D
btnPressed gfx.Tex2D
gui.Rect
}
bird, bg, ground engi.Entity
mask gfx.Color
}
func (sn *StartScene) Load() {
asset.Texture.LoadAtlas("images/bird.png", "images/bird.json")
asset.Font.LoadTrueType("font1", "fonts/Marker Felt.ttf", font.ASCII(32))
}
func (sn *StartScene) OnEnter(g *game.Game) {
font, _ := asset.Font.Get("font1")
gui.SetFont(font)
at, _ := asset.Texture.Atlas("images/bird.png")
bg, _ := at.GetByName("background.png")
ground, _ := at.GetByName("ground.png")
// setup gui
// title
tt, _ := at.GetByName("game_name.png")
sn.title.Tex2D = tt
sn.title.Rect = gui.Rect {
X: (320 - 233)/2,
Y: 80,
W: 233,
H: 70,
}
// start button
btn, _ := at.GetByName("start.png")
sn.start.btnNormal = btn
sn.start.btnPressed = btn
sn.start.Rect = gui.Rect{
X: (320 - 120)/2,
Y: 300,
W: 120,
H: 60,
}
// setup bg
{
entity := korok.Entity.New()
spr := korok.Sprite.NewCompX(entity, bg)
spr.SetSize(320, 480)
xf := korok.Transform.NewComp(entity)
xf.SetPosition( f32.Vec2{160, 240})
sn.bg = entity
}
// setup ground {840 281}
{
entity := korok.Entity.New()
spr := korok.Sprite.NewCompX(entity, ground)
spr.SetSize(420, 140)
spr.SetGravity(0, 1)
spr.SetZOrder(1)
xf := korok.Transform.NewComp(entity)
xf.SetPosition(f32.Vec2{0, 100})
sn.ground = entity
}
// flying animation
bird1, _ := at.GetByName("bird1.png")
bird2, _ := at.GetByName("bird2.png")
bird3, _ := at.GetByName("bird3.png")
frames := []gfx.Tex2D{bird1, bird2, bird3}
g.AnimationSystem.SpriteEngine.NewAnimation("flying", frames, true)
// setup bird
bird := korok.Entity.New()
spr := korok.Sprite.NewCompX(bird, bird1)
spr.SetSize(48, 32)
spr.SetZOrder(2)
xf := korok.Transform.NewComp(bird)
xf.SetPosition(f32.Vec2{160, 240})
anim := korok.Flipbook.NewComp(bird)
anim.SetRate(.1)
anim.Play("flying")
sn.bird = bird
}
func (sn *StartScene) Update(dt float32) {
// draw title
gui.Image(1, sn.title.Rect, sn.title.Tex2D, nil)
// draw start button
e := gui.ImageButton(2, sn.start.Rect, sn.start.btnNormal, sn.start.btnPressed, nil)
if e.JustPressed() {
// sn.LoadGame()
sn.fadeOut()
}
// fade color
if sn.mask.A > 0 {
gui.ColorRect(gui.Rect{W:320,H:480}, sn.mask,0)
}
}
func (sn *StartScene) OnExit() {
}
func (sn *StartScene) fadeOut() {
anim.OfColor(&sn.mask, gfx.Transparent, gfx.White).SetFunction(ease.InOutSine).SetDuration(1).OnComplete(func(reverse bool) {
sn.LoadGame()
}).Forward()
}
func (sn *StartScene) LoadGame() {
gsn := &GameScene{}
gsn.borrow(sn.bird, sn.bg, sn.ground)
// load game scene
korok.SceneMan.Load(gsn)
korok.SceneMan.Push(gsn)
}
func main() {
options := korok.Options{
Title:"Flappy Bird",
Width:320,
Height:480,
}
korok.Run(&options, &StartScene{})
}