/
rabbit_direct_scene.go
166 lines (129 loc) · 3.42 KB
/
rabbit_direct_scene.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
package game
import (
"fmt"
"image/color"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/math/f64"
"github.com/demonodojo/rabbits/assets"
)
const (
screenWidth = 800
screenHeight = 600
lettuceSpawnTime = 1 * time.Second
baseMeteorVelocity = 0.25
meteorSpeedUpAmount = 0.1
meteorSpeedUpTime = 5 * time.Second
)
type RabbitDirectScene struct {
game *Game
camera *Camera
offscreen *ebiten.Image
player *Player
rabbit *Rabbit
lettuceSpawnTimer *Timer
lettuces []*Lettuce
bullets []*Bullet
score int
scale float64
baseVelocity float64
velocityTimer *Timer
}
func NewRabbitDirectScene(g *Game) *RabbitDirectScene {
s := &RabbitDirectScene{
game: g,
camera: &Camera{ViewPort: f64.Vec2{screenWidth, screenHeight}},
lettuceSpawnTimer: NewTimer(lettuceSpawnTime),
baseVelocity: baseMeteorVelocity,
velocityTimer: NewTimer(meteorSpeedUpTime),
}
s.camera.Reset()
s.rabbit = NewRabbit(g)
m := NewLettuce()
s.lettuces = append(s.lettuces, m)
return s
}
func (g *RabbitDirectScene) Update() error {
g.rabbit.Interact()
g.scale += 0.01
if g.scale > 2 {
g.scale = 0.5
}
g.velocityTimer.Update()
if g.velocityTimer.IsReady() {
g.velocityTimer.Reset()
g.baseVelocity += meteorSpeedUpAmount
}
g.rabbit.Update()
g.lettuceSpawnTimer.Update()
if g.lettuceSpawnTimer.IsReady() {
g.lettuceSpawnTimer.Reset()
m := NewLettuce()
g.lettuces = append(g.lettuces, m)
}
for _, l := range g.lettuces {
l.Update()
}
if ebiten.IsKeyPressed(ebiten.Key1) {
ebiten.SetFullscreen(true)
}
if ebiten.IsKeyPressed(ebiten.Key2) {
ebiten.SetFullscreen(false)
}
g.camera.Update(g.rabbit)
// for _, b := range g.bullets {
// b.Update()
// }
// // Check for meteor/bullet collisions
// for i, m := range g.meteors {
// for j, b := range g.bullets {
// if m.Collider().Intersects(b.Collider()) {
// g.meteors = append(g.meteors[:i], g.meteors[i+1:]...)
// g.bullets = append(g.bullets[:j], g.bullets[j+1:]...)
// g.score++
// }
// }
// }
// Check for rabbit/lettuces collisions
for i, l := range g.lettuces {
if l.Collider().Intersects(g.rabbit.Collider()) {
g.lettuces = append(g.lettuces[:i], g.lettuces[i+1:]...)
g.score++
if len(g.lettuces) == 0 {
g.Reset()
}
//g.Reset()
break
}
}
return nil
}
func (g *RabbitDirectScene) Draw(screen *ebiten.Image) {
opts := &ebiten.DrawImageOptions{}
// Ajusta la escala de la imagen. 1 es el tamaño original, valores mayores para hacer zoom in, menores para zoom out.
opts.GeoM.Scale(g.scale, g.scale)
// Dibuja la imagen en la pantalla con las opciones de escala.
g.rabbit.Draw(screen, g.camera.Matrix)
for _, l := range g.lettuces {
l.Draw(screen, g.camera.Matrix)
}
// for _, b := range g.bullets {
// b.Draw(screen)
// }
text.Draw(screen, fmt.Sprintf("%06d", g.score), assets.ScoreFont, screenWidth/2-100, 50, color.White)
}
func (g *RabbitDirectScene) Reset() {
g.rabbit = NewRabbit(g.game)
g.lettuces = nil
g.bullets = nil
g.score = 0
g.lettuceSpawnTimer.Reset()
g.baseVelocity = baseMeteorVelocity
g.velocityTimer.Reset()
}
func (g *RabbitDirectScene) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func (s *RabbitDirectScene) SpawnElement(name string, element interface{}) {
}