Skip to content

Commit 943683f

Browse files
authored
Merge pull request #37 from deggja/feat/consumed-resources
feat: format consumed resource text
2 parents 5245662 + d6cd925 commit 943683f

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
serpent
1+
chaossnake
22
chaos.log

backend/cmd/main.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Food struct {
2929
}
3030

3131
var foodPodMappings []FoodPodMapping
32+
var deletedPodTextEntities []*tl.Text
3233

3334
type FoodPodMapping struct {
3435
foodEntity *Food
@@ -119,6 +120,11 @@ func (snake *Snake) CollidesWithSelf() bool {
119120
}
120121

121122
func GameOver() {
123+
for _, entity := range deletedPodTextEntities {
124+
game.Screen().RemoveEntity(entity)
125+
}
126+
deletedPodTextEntities = nil
127+
122128
showFinalScreen()
123129
log.Println("Game Over!")
124130
}
@@ -183,6 +189,36 @@ func updatePauseTextPosition() {
183189
pauseText.SetPosition(startX, startY)
184190
}
185191

192+
func DeletedResourceText(screen *tl.Screen, resourceType, resourceName, namespace string, startX, startY int) {
193+
for _, entity := range deletedPodTextEntities {
194+
screen.RemoveEntity(entity)
195+
}
196+
deletedPodTextEntities = nil
197+
lines := []struct {
198+
prefix string
199+
value string
200+
}{
201+
{"Resource: ", resourceType},
202+
{"Name: ", resourceName},
203+
{"Namespace: ", namespace},
204+
}
205+
206+
generalMessage := "Oh no! Seems like the snake ate something.."
207+
generalMessageEntity := tl.NewText(startX, startY, generalMessage, tl.ColorWhite, tl.ColorBlack)
208+
screen.AddEntity(generalMessageEntity)
209+
deletedPodTextEntities = append(deletedPodTextEntities, generalMessageEntity)
210+
211+
for i, line := range lines {
212+
prefixEntity := tl.NewText(startX, startY+1+i, line.prefix, tl.ColorWhite, tl.ColorBlack)
213+
valueEntity := tl.NewText(startX+len(line.prefix), startY+1+i, line.value, tl.ColorGreen, tl.ColorBlack)
214+
215+
screen.AddEntity(prefixEntity)
216+
screen.AddEntity(valueEntity)
217+
218+
deletedPodTextEntities = append(deletedPodTextEntities, prefixEntity, valueEntity)
219+
}
220+
}
221+
186222
var score int
187223

188224
func (snake *Snake) Tick(event tl.Event) {
@@ -192,13 +228,11 @@ func (snake *Snake) Tick(event tl.Event) {
192228
if isPaused {
193229
updatePauseTextPosition()
194230
} else {
195-
// Move text off-screen when unpaused
196231
pauseText.SetPosition(-1, -1)
197232
}
198-
return // Return early to avoid processing other inputs or game logic
233+
return
199234
}
200235

201-
// If the game is paused, skip updating the game logic
202236
if isPaused {
203237
return
204238
}
@@ -248,14 +282,18 @@ func (snake *Snake) Tick(event tl.Event) {
248282
food.placed = false
249283
score++
250284
scoreText.SetText(fmt.Sprintf("Score: %d", score))
251-
252-
// Handle resource deletion linked to food
253285
for index, mapping := range foodPodMappings {
254286
if mapping.foodEntity == food {
255287
go k8s.DeleteResource(mapping.resourceInfo)
256-
deletionMessage := fmt.Sprintf("Oh no! Seems like you ate %s: %s in namespace %s", mapping.resourceInfo.Type, mapping.resourceInfo.Name, mapping.resourceInfo.Namespace)
257-
deletedPodText.SetText(deletionMessage)
258-
log.Println(deletionMessage)
288+
DeletedResourceText(
289+
game.Screen(),
290+
mapping.resourceInfo.Type,
291+
mapping.resourceInfo.Name,
292+
mapping.resourceInfo.Namespace,
293+
1,
294+
LevelHeight+0,
295+
)
296+
log.Printf("Chaos snake consumed %s: %s in namespace %s\n", mapping.resourceInfo.Type, mapping.resourceInfo.Name, mapping.resourceInfo.Namespace)
259297
foodPodMappings = append(foodPodMappings[:index], foodPodMappings[index+1:]...)
260298
break
261299
}

0 commit comments

Comments
 (0)