laneya2

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

commit
44febec7a7107997f7df7d9d4731b5e685eb5ba3
parent
e418bee4f469e12763fc06e3e4404232713581c1
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-10-04 16:46
refactor: check item before removing

Diffstat

M player.go 21 +++++++++++----------

1 files changed, 11 insertions, 10 deletions


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

@@ -77,12 +77,10 @@ func (player *Player) AddItem(item string, amount uint) {
   77    77 	})
   78    78 }
   79    79 
   80    -1 func (player *Player) RemoveItem(item string) bool {
   -1    80 func (player *Player) RemoveItem(item string) {
   81    81 	value, ok := player.Inventory[item]
   82    -1 	success := true
   83    82 	if !ok {
   84    83 		value = 0
   85    -1 		success = false
   86    84 	} else if value > 1 {
   87    85 		value -= 1
   88    86 		player.Inventory[item] = value
@@ -96,8 +94,6 @@ func (player *Player) RemoveItem(item string) bool {
   96    94 		"item":   item,
   97    95 		"amount": value,
   98    96 	})
   99    -1 
  100    -1 	return success
  101    97 }
  102    98 
  103    99 func (player *Player) Move(dir string) {
@@ -134,17 +130,22 @@ func (player *Player) PickupItems() {
  134   130 }
  135   131 
  136   132 func (player *Player) DropItem(item string) {
  137    -1 	if player.RemoveItem(item) {
   -1   133 	if _, ok := player.Inventory[item]; ok {
   -1   134 		player.RemoveItem(item)
  138   135 		player.Game.addToPile(player.Pos, item, 1)
  139   136 	}
  140   137 }
  141   138 
  142   139 func (player *Player) UseItem(item string) {
  143    -1 	if item == "potion" {
   -1   140 	if _, ok := player.Inventory[item]; !ok {
   -1   141 		return
   -1   142 	}
   -1   143 
   -1   144 	switch item {
   -1   145 	case "potion":
  144   146 		if player.Health < player.HealthTotal {
  145    -1 			if player.RemoveItem(item) {
  146    -1 				player.Heal(10)
  147    -1 			}
   -1   147 			player.RemoveItem(item)
   -1   148 			player.Heal(10)
  148   149 		}
  149   150 	}
  150   151 }