185 lines
4.3 KiB
Go
185 lines
4.3 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"git.zb-server.de/ZB-Server/EscapeFromTeacher/pkg/game"
|
|
)
|
|
|
|
// CheckCoinCollision prüft ob Spieler Coins einsammelt
|
|
func (r *Room) CheckCoinCollision(p *ServerPlayer) {
|
|
if !p.IsAlive || p.IsSpectator {
|
|
return
|
|
}
|
|
|
|
playerHitbox := game.Rect{
|
|
OffsetX: p.X + r.pDrawOffX + r.pHitboxOffX,
|
|
OffsetY: p.Y + r.pDrawOffY + r.pHitboxOffY,
|
|
W: r.pW,
|
|
H: r.pH,
|
|
}
|
|
|
|
// Durch alle aktiven Chunks iterieren
|
|
for _, activeChunk := range r.ActiveChunks {
|
|
chunkDef, exists := r.World.ChunkLibrary[activeChunk.ChunkID]
|
|
if !exists {
|
|
continue
|
|
}
|
|
|
|
// Durch alle Objekte im Chunk
|
|
for objIdx, obj := range chunkDef.Objects {
|
|
assetDef, ok := r.World.Manifest.Assets[obj.AssetID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
// Nur Coins prüfen
|
|
if assetDef.Type != "coin" {
|
|
continue
|
|
}
|
|
|
|
// Eindeutiger Key für diesen Coin
|
|
coinKey := fmt.Sprintf("%s_%d", activeChunk.ChunkID, objIdx)
|
|
|
|
// Wurde bereits eingesammelt?
|
|
if r.CollectedCoins[coinKey] {
|
|
continue
|
|
}
|
|
|
|
// Coin-Hitbox (muss DrawOffX/Y einbeziehen wie bei Obstacles!)
|
|
coinHitbox := game.Rect{
|
|
OffsetX: activeChunk.X + obj.X + assetDef.DrawOffX + assetDef.Hitbox.OffsetX,
|
|
OffsetY: obj.Y + assetDef.DrawOffY + assetDef.Hitbox.OffsetY,
|
|
W: assetDef.Hitbox.W,
|
|
H: assetDef.Hitbox.H,
|
|
}
|
|
|
|
// Kollision?
|
|
if game.CheckRectCollision(playerHitbox, coinHitbox) {
|
|
// Coin einsammeln!
|
|
r.CollectedCoins[coinKey] = true
|
|
p.Score += 200
|
|
log.Printf("💰 %s hat Coin eingesammelt! Score: %d", p.Name, p.Score)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// CheckPowerupCollision prüft ob Spieler Powerups einsammelt
|
|
func (r *Room) CheckPowerupCollision(p *ServerPlayer) {
|
|
if !p.IsAlive || p.IsSpectator {
|
|
return
|
|
}
|
|
|
|
playerHitbox := game.Rect{
|
|
OffsetX: p.X + r.pDrawOffX + r.pHitboxOffX,
|
|
OffsetY: p.Y + r.pDrawOffY + r.pHitboxOffY,
|
|
W: r.pW,
|
|
H: r.pH,
|
|
}
|
|
|
|
// Durch alle aktiven Chunks iterieren
|
|
for _, activeChunk := range r.ActiveChunks {
|
|
chunkDef, exists := r.World.ChunkLibrary[activeChunk.ChunkID]
|
|
if !exists {
|
|
continue
|
|
}
|
|
|
|
// Durch alle Objekte im Chunk
|
|
for objIdx, obj := range chunkDef.Objects {
|
|
assetDef, ok := r.World.Manifest.Assets[obj.AssetID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
// Nur Powerups prüfen
|
|
if assetDef.Type != "powerup" {
|
|
continue
|
|
}
|
|
|
|
// Eindeutiger Key für dieses Powerup
|
|
powerupKey := fmt.Sprintf("%s_%d", activeChunk.ChunkID, objIdx)
|
|
|
|
// Wurde bereits eingesammelt?
|
|
if r.CollectedPowerups[powerupKey] {
|
|
continue
|
|
}
|
|
|
|
// Powerup-Hitbox
|
|
powerupHitbox := game.Rect{
|
|
OffsetX: activeChunk.X + obj.X + assetDef.DrawOffX + assetDef.Hitbox.OffsetX,
|
|
OffsetY: obj.Y + assetDef.DrawOffY + assetDef.Hitbox.OffsetY,
|
|
W: assetDef.Hitbox.W,
|
|
H: assetDef.Hitbox.H,
|
|
}
|
|
|
|
// Kollision?
|
|
if game.CheckRectCollision(playerHitbox, powerupHitbox) {
|
|
// Powerup einsammeln!
|
|
r.CollectedPowerups[powerupKey] = true
|
|
|
|
// Powerup-Effekt anwenden
|
|
switch obj.AssetID {
|
|
case "jumpboost":
|
|
p.HasDoubleJump = true
|
|
p.DoubleJumpUsed = false
|
|
log.Printf("⚡ %s hat Double Jump erhalten!", p.Name)
|
|
|
|
case "godmode":
|
|
p.HasGodMode = true
|
|
p.GodModeEndTime = time.Now().Add(10 * time.Second)
|
|
log.Printf("🛡️ %s hat Godmode erhalten! (10 Sekunden)", p.Name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// UpdateDistanceScore aktualisiert Distanz-basierte Punkte
|
|
func (r *Room) UpdateDistanceScore() {
|
|
if r.Status != "RUNNING" {
|
|
return
|
|
}
|
|
|
|
// Jeder Spieler bekommt Punkte basierend auf seiner eigenen Distanz
|
|
// Punkte = (X-Position / TileSize) = Distanz in Tiles
|
|
for _, p := range r.Players {
|
|
if p.IsAlive && !p.IsSpectator {
|
|
// Berechne Score basierend auf X-Position
|
|
// 1 Punkt pro Tile (64px)
|
|
newScore := int(p.X / 64.0)
|
|
|
|
// Nur updaten wenn höher als aktueller Score
|
|
if newScore > p.Score {
|
|
p.Score = newScore
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// KillPlayer markiert Spieler als tot
|
|
func (r *Room) KillPlayer(p *ServerPlayer) {
|
|
if !p.IsAlive {
|
|
return
|
|
}
|
|
|
|
p.IsAlive = false
|
|
p.IsSpectator = true
|
|
log.Printf("💀 %s ist gestorben! Final Score: %d", p.Name, p.Score)
|
|
|
|
// Prüfen ob alle tot sind
|
|
aliveCount := 0
|
|
for _, pl := range r.Players {
|
|
if pl.IsAlive && !pl.IsSpectator {
|
|
aliveCount++
|
|
}
|
|
}
|
|
|
|
if aliveCount == 0 && r.Status == "RUNNING" {
|
|
log.Printf("🏁 Alle Spieler tot - Game Over!")
|
|
r.Status = "GAMEOVER"
|
|
}
|
|
}
|