/
client_scene.go
231 lines (193 loc) · 5.21 KB
/
client_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
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
package game
import (
"encoding/json"
"fmt"
"image/color"
"log"
"github.com/google/uuid"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/math/f64"
"github.com/demonodojo/rabbits/assets"
"github.com/demonodojo/rabbits/game/network"
)
type ClientScene struct {
game *Game
camera *Camera
client network.GenericClient
rabbit *Rabbit
rabbits map[uuid.UUID]*Rabbit
lettuces map[uuid.UUID]*Lettuce
bullets map[uuid.UUID]*Bullet
rabbitsOrder []uuid.UUID
lettucesOrder []uuid.UUID
bulletsOrder []uuid.UUID
score int
scale float64
baseVelocity float64
velocityTimer *Timer
}
func NewClientScene(g *Game, client network.GenericClient) *ClientScene {
s := &ClientScene{
game: g,
camera: &Camera{ViewPort: f64.Vec2{screenWidth, screenHeight}},
client: client,
baseVelocity: baseMeteorVelocity,
velocityTimer: NewTimer(meteorSpeedUpTime),
}
s.camera.Reset()
s.rabbits = make(map[uuid.UUID]*Rabbit)
s.lettuces = make(map[uuid.UUID]*Lettuce)
s.bullets = make(map[uuid.UUID]*Bullet)
s.rabbit = NewRabbit(g)
s.rabbits[s.rabbit.ID] = s.rabbit
s.UpdateRabbitsOrder()
s.UpdateLettucesOrder()
s.UpdateBulletsOrder()
client.Write(s.rabbit.ToJson())
return s
}
func (s *ClientScene) Update() error {
if s.rabbit.Interact() {
s.client.Write(s.rabbit.ToJson())
s.rabbit.Action = "NONE"
}
s.UpdateRabbits()
for _, r := range s.rabbits {
r.Update()
}
s.camera.Update(s.rabbit)
for _, l := range s.lettuces {
l.Update()
if l.Action == "Delete" {
delete(s.lettuces, l.ID)
s.UpdateLettucesOrder()
}
}
for _, b := range s.bullets {
b.Update()
if b.Action == "DELETE" {
delete(s.bullets, b.ID)
s.UpdateBulletsOrder()
}
}
if ebiten.IsKeyPressed(ebiten.Key1) {
ebiten.SetFullscreen(true)
}
if ebiten.IsKeyPressed(ebiten.Key2) {
ebiten.SetFullscreen(false)
}
return nil
}
func (g *ClientScene) 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.
for _, id := range g.rabbitsOrder {
r := g.rabbits[id]
r.Draw(screen, g.camera.Matrix)
}
for _, id := range g.lettucesOrder {
l := g.lettuces[id]
l.Draw(screen, g.camera.Matrix)
}
for _, id := range g.bulletsOrder {
l := g.bullets[id]
l.Draw(screen, g.camera.Matrix)
}
text.Draw(screen, fmt.Sprintf("%06d", g.rabbit.Score), assets.ScoreFont, screenWidth/2-100, 50, color.White)
text.Draw(screen, fmt.Sprintf("%06d", len(g.bullets)), assets.InfoFont, 10, 50, color.White)
}
func (g *ClientScene) Reset() {
g.rabbit = NewRabbit(g.game)
g.lettuces = nil
g.score = 0
g.baseVelocity = baseMeteorVelocity
g.velocityTimer.Reset()
}
func (g *ClientScene) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func (s *ClientScene) SpawnElement(name string, element interface{}) {
}
func (s *ClientScene) UpdateRabbits() {
messages := s.client.ReadAll()
for _, m := range messages {
jsonData := []byte(m)
var serial Serial
if err := json.Unmarshal(jsonData, &serial); err != nil {
log.Fatal(fmt.Errorf("Cannot unmarshal %s", m))
continue
}
switch serial.ClassName {
case "Rabbit":
var rabbit Rabbit
if err := json.Unmarshal(jsonData, &rabbit); err != nil {
log.Fatal(fmt.Errorf("Cannot unmarshal the Rabbit %s", m))
continue
}
var existing *Rabbit
if s.rabbit.ID == rabbit.ID {
existing = s.rabbit
s.rabbit.Score = rabbit.Score
s.rabbit.Speed = rabbit.Speed
if EuclidianDistance(rabbit.Position, s.rabbit.Position) > 100.0 {
s.rabbit.Position = rabbit.Position
}
continue
} else {
existing = s.rabbits[rabbit.ID]
}
if existing != nil {
existing.CopyFrom(&rabbit)
} else {
newRabbit := NewRabbit(s.game)
newRabbit.CopyFrom(&rabbit)
s.rabbits[rabbit.ID] = newRabbit
s.UpdateRabbitsOrder()
}
case "Lettuce":
var lettuce Lettuce
if err := json.Unmarshal(jsonData, &lettuce); err != nil {
log.Fatal(fmt.Errorf("Cannot unmarshal the Lettuce %s", m))
continue
}
existing := s.lettuces[lettuce.ID]
if existing != nil {
existing.CopyFrom(&lettuce)
} else {
l := NewLettuce()
l.CopyFrom(&lettuce)
s.lettuces[lettuce.ID] = l
s.UpdateLettucesOrder()
}
case "Bullet":
var bullet Bullet
if err := json.Unmarshal(jsonData, &bullet); err != nil {
log.Fatal(fmt.Errorf("Cannot unmarshal the Bullet %s", m))
continue
}
existing := s.bullets[bullet.ID]
if existing != nil {
existing.CopyFrom(&bullet)
} else {
b := NewBullet(bullet.Position, bullet.Rotation)
b.CopyFrom(&bullet)
s.bullets[bullet.ID] = b
s.UpdateBulletsOrder()
}
default:
log.Printf("Cannot unmarshal the Message %s", m)
}
}
}
func (s *ClientScene) UpdateRabbitsOrder() {
s.rabbitsOrder = GetOrderedIds(s.rabbits)
}
func (s *ClientScene) UpdateLettucesOrder() {
s.lettucesOrder = GetOrderedIds(s.lettuces)
}
func (s *ClientScene) UpdateBulletsOrder() {
s.bulletsOrder = GetOrderedIds(s.bullets)
}