laneya2

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

commit
b423166e746fe9db5c359027d4add2d678f1e5b6
parent
c5f423649b6fc21a2dca0bf656d9f5963b79b373
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-29 07:50
prevent player from using items they don't have

Diffstat

M player.go 17 +++++++++++------

1 files changed, 11 insertions, 6 deletions


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

@@ -64,8 +64,9 @@ func (player *Player) AddItem(item string, amount uint) {
   64    64 	}
   65    65 }
   66    66 
   67    -1 func (player *Player) RemoveItem(item string, amount uint) {
   -1    67 func (player *Player) RemoveItem(item string, amount uint) bool {
   68    68 	value, ok := player.Inventory[item]
   -1    69 	success := false
   69    70 	if !ok {
   70    71 		value = 0
   71    72 	} else if value <= amount {
@@ -74,6 +75,7 @@ func (player *Player) RemoveItem(item string, amount uint) {
   74    75 	} else {
   75    76 		value -= amount
   76    77 		player.Inventory[item] = value
   -1    78 		success = true
   77    79 	}
   78    80 
   79    81 	player.Send <- []Message{
@@ -83,6 +85,8 @@ func (player *Player) RemoveItem(item string, amount uint) {
   83    85 			"amount": value,
   84    86 		},
   85    87 	}
   -1    88 
   -1    89 	return success
   86    90 }
   87    91 
   88    92 func (player *Player) Move(dir string) {
@@ -123,17 +127,18 @@ func (player *Player) PickupItems() {
  123   127 }
  124   128 
  125   129 func (player *Player) DropItem(item string) {
  126    -1 	player.RemoveItem(item, 1)
  127    -1 	player.Game.addToPile(player.Pos, item)
   -1   130 	if player.RemoveItem(item, 1) {
   -1   131 		player.Game.addToPile(player.Pos, item)
   -1   132 	}
  128   133 }
  129   134 
  130   135 func (player *Player) UseItem(item string) {
  131   136 	// TODO: send result in a single transaction
  132    -1 	// TODO: check if item is in inventory
  133   137 	if item == "potion" {
  134   138 		if player.Health < player.HealthTotal {
  135    -1 			player.RemoveItem(item, 1)
  136    -1 			player.Heal(10)
   -1   139 			if player.RemoveItem(item, 1) {
   -1   140 				player.Heal(10)
   -1   141 			}
  137   142 		}
  138   143 	}
  139   144 }