-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
225 lines (180 loc) · 4.72 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
//go:generate file2byteslice -package=images -input=images/survivor1_gun.png -output=./images/survivor.go -var=Survivor_png
//go:generate file2byteslice -package=images -input=images/zombie1_hold.png -output=./images/zombie1.go -var=Zombie1_png
//go:generate file2byteslice -package=sound -input=sound/all.mp3 -output=./sound/all.go -var=All_mp3
package main
import (
"bytes"
"fmt"
"image"
"image/color"
"log"
"math"
"time"
"github.com/dosko64/lifewreck/images"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
const (
// Settings
screenWidth = 800
screenHeight = 800
sampleRate = 44100
Running = iota
Paused
)
var Player player
var bulletSprite *ebiten.Image
var enemySprite *ebiten.Image
var Score int
var Health = 15
var HighScore int
var state = Running
// var audioContext *audio.Context
// var audioPlayer *audio.Player
func init() {
// // Create audio player
// var err error
// audioContext, err = audio.NewContext(sampleRate)
// if err != nil {
// log.Fatal(err)
// }
// Preload images
img, _, err := image.Decode(bytes.NewReader(images.Survivor_png))
if err != nil {
log.Fatal("Could not open file: ", err)
}
playerSprite, err = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err != nil {
log.Fatal("Could not create player sprite")
}
width, heigth := playerSprite.Size()
bulletSprite, err = ebiten.NewImage(5, 5, ebiten.FilterNearest)
if err != nil {
log.Fatal("Could not create dot image: ", err)
}
bulletSprite.Fill(color.White)
// Create player
Player = player{
x: screenWidth / 2,
y: screenHeight / 2,
height: float64(heigth) / 2,
width: float64(width) / 2,
LastShoot: time.Now().Add(-1 * time.Second),
BulletSpeed: 300,
Speed: 3,
}
img, _, err = image.Decode(bytes.NewReader(images.Zombie1_png))
if err != nil {
log.Fatal("Could not open file: ", err)
}
enemySprite, err = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err != nil {
log.Fatal("Could not create enemy sprite")
}
initBullets()
initEnemies()
}
var lastEnemy = time.Now()
func update(screen *ebiten.Image) error {
// if audioPlayer == nil {
// // Decode the wav file.
// // wavS is a decoded io.ReadCloser and io.Seeker.
// wavS, err := mp3.Decode(audioContext, audio.BytesReadSeekCloser(sound.All_mp3))
// if err != nil {
// return err
// }
// // Create an infinite loop stream from the decoded bytes.
// // s is still an io.ReadCloser and io.Seeker.
// s := audio.NewInfiniteLoop(wavS, wavS.Length())
// audioPlayer, err = audio.NewPlayer(audioContext, s)
// if err != nil {
// return err
// }
// // Play the infinite-length stream. This never ends.
// audioPlayer.Play()
// }
if Health < 0 {
if Score > HighScore {
HighScore = Score
}
Health = 15
Score = 0
}
if ebiten.IsKeyPressed(ebiten.KeyQ) && state == Running {
state = Paused
}
if ebiten.IsKeyPressed(ebiten.KeyQ) && state == Paused {
state = Running
}
if state == Paused {
return nil
}
Player.Move()
// Find mouse position
x, y := ebiten.CursorPosition()
// Find angle to mouse cursor
Player.Angle = math.Atan2(float64(y)-Player.Y(), float64(x)-Player.X())
// Check if mouse is pressed and shoot
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
if time.Now().Sub(Player.LastShoot) > 1*time.Second {
Player.Shoot()
}
}
for t, b := range bullets {
if b.deleted == true {
delete(bullets, t)
}
b.Update()
b.Draw(screen)
}
// Check if we are near screen sides
Player.CheckScreen(screenWidth, screenHeight)
if ebiten.IsRunningSlowly() {
return nil
}
Player.Draw(screen)
// Draw bullets
// space.Step(1.0 / ebiten.FPS)
if time.Now().Sub(lastEnemy) > 1*time.Second {
NewEnemy((Player.X()+Player.SX())/2, (Player.Y()+Player.SY())/2)
lastEnemy = time.Now()
}
for t, e := range enemies {
for tt, ee := range enemies {
if e.CheckCollision(ee) {
if t != tt {
e.Speed = 0.5
}
}
}
if e.CheckCollision(Player) {
delete(enemies, t)
Health--
}
for _, b := range bullets {
if e.CheckCollision(b) {
delete(enemies, t)
Score++
}
}
e.Update((Player.X()+Player.SX())/2, (Player.Y()+Player.SY())/2)
e.Draw(screen)
}
// FPS counter
// ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS()))
ebitenutil.DebugPrint(screen, fmt.Sprintf("\nScore: %d", Score))
ebitenutil.DebugPrint(screen, fmt.Sprintf("\n\nHealth: %d", Health))
ebitenutil.DebugPrint(screen, fmt.Sprintf("\n\n\nHighscore: %d", HighScore))
return nil
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Lifewreck"); err != nil {
panic(err)
}
}
type Something interface {
X() float64
Y() float64
SY() float64
SX() float64
}