laneya2

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

commit
c5f423649b6fc21a2dca0bf656d9f5963b79b373
parent
914ed010da01fab8fecf64fd21be5e9cd3f11016
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-29 07:44
refactor: call methods from Game.run

Diffstat

M game.go 61 +++++++++++++++++++------------------------------------------
M monster.go 30 +++++++++++++++---------------
M player.go 63 ++++++++++++++++++++++++++++++++++++++++--------------------

3 files changed, 76 insertions, 78 deletions


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

@@ -289,56 +289,33 @@ func (game *Game) run() {
  289   289 					},
  290   290 				})
  291   291 			}
  292    -1 		case cmsg := <-game.Msg:
  293    -1 			player := cmsg.Player
  294    -1 			msg := cmsg.Msg
  295    -1 
  296    -1 			if msg["action"] == "move" {
  297    -1 				dir, ok := msg["dir"].(string)
  298    -1 				if !ok {
  299    -1 					continue
  300    -1 				}
  301    -1 				player.Move(dir)
  302    -1 			} else if msg["action"] == "pickup" {
  303    -1 				pile, ok := game.Piles[player.Pos]
   -1   292 		case pmsg := <-game.Msg:
   -1   293 			if pmsg.Msg["action"] == "move" {
   -1   294 				dir, ok := pmsg.Msg["dir"].(string)
  304   295 				if ok {
  305    -1 					delete(game.Piles, player.Pos)
  306    -1 					for item, amount := range pile.Items {
  307    -1 						player.AddItem(item, amount)
  308    -1 					}
  309    -1 					game.broadcast([]Message{
  310    -1 						Message{
  311    -1 							"action": "remove",
  312    -1 							"id":     pile.Id,
  313    -1 						},
  314    -1 					})
   -1   296 					pmsg.Player.Move(dir)
  315   297 				}
  316    -1 			} else if msg["action"] == "drop" {
  317    -1 				item, ok := msg["item"].(string)
  318    -1 				if !ok {
  319    -1 					continue
   -1   298 			} else if pmsg.Msg["action"] == "pickup" {
   -1   299 				pmsg.Player.PickupItems()
   -1   300 			} else if pmsg.Msg["action"] == "drop" {
   -1   301 				item, ok := pmsg.Msg["item"].(string)
   -1   302 				if ok {
   -1   303 					pmsg.Player.DropItem(item)
  320   304 				}
  321    -1 				player.RemoveItem(item, 1)
  322    -1 				game.addToPile(player.Pos, item)
  323    -1 			} else if msg["action"] == "use" {
  324    -1 				item, ok := msg["item"].(string)
  325    -1 				if !ok {
  326    -1 					continue
   -1   305 			} else if pmsg.Msg["action"] == "use" {
   -1   306 				item, ok := pmsg.Msg["item"].(string)
   -1   307 				if ok {
   -1   308 					pmsg.Player.UseItem(item)
  327   309 				}
  328    -1 				player.UseItem(item)
  329   310 			} else if verbose {
  330    -1 				log.Println("unknown action", msg)
   -1   311 				log.Println("unknown action", pmsg.Msg)
  331   312 			}
  332   313 		case mmsg := <-game.MMsg:
  333    -1 			monster := mmsg.Monster
  334    -1 			msg := mmsg.Msg
  335    -1 
  336    -1 			if msg["action"] == "move" {
  337    -1 				dir, ok := msg["dir"].(string)
  338    -1 				if !ok {
  339    -1 					continue
   -1   314 			if mmsg.Msg["action"] == "move" {
   -1   315 				dir, ok := mmsg.Msg["dir"].(string)
   -1   316 				if ok {
   -1   317 					mmsg.Monster.Move(dir)
  340   318 				}
  341    -1 				monster.Move(dir)
  342   319 			}
  343   320 		}
  344   321 	}

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

@@ -69,6 +69,21 @@ func (monster *Monster) run() {
   69    69 	}
   70    70 }
   71    71 
   -1    72 func (monster *Monster) TakeDamage(amount int) {
   -1    73 	monster.Health -= amount
   -1    74 	if monster.Health <= 0 {
   -1    75 		monster.quit <- true
   -1    76 		delete(monster.Game.Monsters, monster)
   -1    77 		monster.Game.addToPile(monster.Pos, "potion")
   -1    78 		monster.Game.broadcast([]Message{
   -1    79 			Message{
   -1    80 				"action": "remove",
   -1    81 				"id":     monster.Id,
   -1    82 			},
   -1    83 		})
   -1    84 	}
   -1    85 }
   -1    86 
   72    87 func (monster *Monster) Move(dir string) {
   73    88 	game := monster.Game
   74    89 	pos := monster.Pos.Move(dir)
@@ -86,18 +101,3 @@ func (monster *Monster) Move(dir string) {
   86   101 		})
   87   102 	}
   88   103 }
   89    -1 
   90    -1 func (monster *Monster) TakeDamage(amount int) {
   91    -1 	monster.Health -= amount
   92    -1 	if monster.Health <= 0 {
   93    -1 		monster.quit <- true
   94    -1 		delete(monster.Game.Monsters, monster)
   95    -1 		monster.Game.addToPile(monster.Pos, "potion")
   96    -1 		monster.Game.broadcast([]Message{
   97    -1 			Message{
   98    -1 				"action": "remove",
   99    -1 				"id":     monster.Id,
  100    -1 			},
  101    -1 		})
  102    -1 	}
  103    -1 }

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

@@ -20,26 +20,6 @@ type PlayerMessage struct {
   20    20 	Msg    Message
   21    21 }
   22    22 
   23    -1 func (player *Player) Move(dir string) {
   24    -1 	game := player.Game
   25    -1 	pos := player.Pos.Move(dir)
   26    -1 	monster := game.getMonsterAt(pos)
   27    -1 	if monster != nil {
   28    -1 		monster.TakeDamage(5)
   29    -1 	} else if game.IsFree(pos) {
   30    -1 		player.Pos = pos
   31    -1 		game.broadcast([]Message{
   32    -1 			Message{
   33    -1 				"action": "setPosition",
   34    -1 				"id":     player.Id,
   35    -1 				"pos":    player.Pos,
   36    -1 			},
   37    -1 		})
   38    -1 
   39    -1 		game.MaybeNextLevel()
   40    -1 	}
   41    -1 }
   42    -1 
   43    23 func (player *Player) TakeDamage(amount uint) {
   44    24 	// TODO: death if amount >= player.Health
   45    25 	player.Health -= amount
@@ -53,7 +33,6 @@ func (player *Player) TakeDamage(amount uint) {
   53    33 }
   54    34 
   55    35 func (player *Player) Heal(amount uint) {
   56    -1 	// TODO: death if amount >= player.Health
   57    36 	player.Health += amount
   58    37 	if player.Health > player.HealthTotal {
   59    38 		player.Health = player.HealthTotal
@@ -106,6 +85,48 @@ func (player *Player) RemoveItem(item string, amount uint) {
  106    85 	}
  107    86 }
  108    87 
   -1    88 func (player *Player) Move(dir string) {
   -1    89 	game := player.Game
   -1    90 	pos := player.Pos.Move(dir)
   -1    91 	monster := game.getMonsterAt(pos)
   -1    92 	if monster != nil {
   -1    93 		monster.TakeDamage(5)
   -1    94 	} else if game.IsFree(pos) {
   -1    95 		player.Pos = pos
   -1    96 		game.broadcast([]Message{
   -1    97 			Message{
   -1    98 				"action": "setPosition",
   -1    99 				"id":     player.Id,
   -1   100 				"pos":    player.Pos,
   -1   101 			},
   -1   102 		})
   -1   103 
   -1   104 		game.MaybeNextLevel()
   -1   105 	}
   -1   106 }
   -1   107 
   -1   108 func (player *Player) PickupItems() {
   -1   109 	game := player.Game
   -1   110 	pile, ok := game.Piles[player.Pos]
   -1   111 	if ok {
   -1   112 		delete(game.Piles, player.Pos)
   -1   113 		for item, amount := range pile.Items {
   -1   114 			player.AddItem(item, amount)
   -1   115 		}
   -1   116 		game.broadcast([]Message{
   -1   117 			Message{
   -1   118 				"action": "remove",
   -1   119 				"id":     pile.Id,
   -1   120 			},
   -1   121 		})
   -1   122 	}
   -1   123 }
   -1   124 
   -1   125 func (player *Player) DropItem(item string) {
   -1   126 	player.RemoveItem(item, 1)
   -1   127 	player.Game.addToPile(player.Pos, item)
   -1   128 }
   -1   129 
  109   130 func (player *Player) UseItem(item string) {
  110   131 	// TODO: send result in a single transaction
  111   132 	// TODO: check if item is in inventory