laneya2

cave exploration game
git clone https://git.ce9e.org/laneya2.git

commit
b579a01f3a3d55ad32530060ce6f8df5248e5205
parent
c0008e4afea67cbc431c575ee9a79bd06cb80f7a
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-27 06:51
add fight

Diffstat

M monster.go 16 +++++++++++++++-
M player.go 13 ++++++++++++-

2 files changed, 27 insertions, 2 deletions


diff --git a/monster.go b/monster.go

@@ -71,7 +71,7 @@ func (monster *Monster) Move(dir string) {
   71    71 	pos := monster.Pos.Move(dir)
   72    72 	player := game.getPlayerAt(pos)
   73    73 	if player != nil {
   74    -1 		// TODO
   -1    74 		player.TakeDamage(2)
   75    75 	} else if game.getMonsterAt(pos) == nil && game.IsFree(pos) {
   76    76 		monster.Pos = pos
   77    77 		game.broadcast([]Message{
@@ -83,3 +83,17 @@ func (monster *Monster) Move(dir string) {
   83    83 		})
   84    84 	}
   85    85 }
   -1    86 
   -1    87 func (monster *Monster) TakeDamage(amount int) {
   -1    88 	monster.Health -= amount
   -1    89 	if monster.Health <= 0 {
   -1    90 		monster.quit <- true
   -1    91 		delete(monster.Game.Monsters, monster)
   -1    92 		monster.Game.broadcast([]Message{
   -1    93 			Message{
   -1    94 				"action": "remove",
   -1    95 				"id":     monster.Id,
   -1    96 			},
   -1    97 		})
   -1    98 	}
   -1    99 }

diff --git a/player.go b/player.go

@@ -24,7 +24,7 @@ func (player *Player) Move(dir string) {
   24    24 	pos := player.Pos.Move(dir)
   25    25 	monster := game.getMonsterAt(pos)
   26    26 	if monster != nil {
   27    -1 		// TODO
   -1    27 		monster.TakeDamage(5)
   28    28 	} else if game.IsFree(pos) {
   29    29 		player.Pos = pos
   30    30 		game.broadcast([]Message{
@@ -38,3 +38,14 @@ func (player *Player) Move(dir string) {
   38    38 		game.MaybeNextLevel()
   39    39 	}
   40    40 }
   -1    41 
   -1    42 func (player *Player) TakeDamage(amount int) {
   -1    43 	player.Health -= amount
   -1    44 	player.Game.broadcast([]Message{
   -1    45 		Message{
   -1    46 			"action":      "setHealth",
   -1    47 			"health":      player.Health,
   -1    48 			"healthTotal": player.HealthTotal,
   -1    49 		},
   -1    50 	})
   -1    51 }