laneya2

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

commit
16b95848ae8cb675bc7d6e82088c12c8775b35ea
parent
0319bf091cd38df43ff35ad0930d35117ba84efc
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-28 16:32
implement server-side inventory

Diffstat

M game.go 16 ++++++++++++++++
M player.go 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M server.go 1 +
M static/main.js 14 +++++---------

4 files changed, 88 insertions, 9 deletions


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

@@ -252,6 +252,22 @@ func (game *Game) run() {
  252   252 					continue
  253   253 				}
  254   254 				player.Move(dir)
   -1   255 			} else if msg["action"] == "pickup" {
   -1   256 				// TODO only if there is loot on player.Pos
   -1   257 				player.AddItem("potion", 1)
   -1   258 			} else if msg["action"] == "drop" {
   -1   259 				item, ok := msg["item"].(string)
   -1   260 				if !ok {
   -1   261 					continue
   -1   262 				}
   -1   263 				// TODO: add to loot pile
   -1   264 				player.RemoveItem(item, 1)
   -1   265 			} else if msg["action"] == "use" {
   -1   266 				item, ok := msg["item"].(string)
   -1   267 				if !ok {
   -1   268 					continue
   -1   269 				}
   -1   270 				player.UseItem(item)
  255   271 			} else if verbose {
  256   272 				log.Println("unknown action", msg)
  257   273 			}

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

@@ -12,6 +12,7 @@ type Player struct {
   12    12 	Speed       float32
   13    13 	Health      uint
   14    14 	HealthTotal uint
   -1    15 	Inventory   map[string]uint
   15    16 }
   16    17 
   17    18 type PlayerMessage struct {
@@ -50,3 +51,68 @@ func (player *Player) TakeDamage(amount uint) {
   50    51 		},
   51    52 	}
   52    53 }
   -1    54 
   -1    55 func (player *Player) Heal(amount uint) {
   -1    56 	// TODO: death if amount >= player.Health
   -1    57 	player.Health += amount
   -1    58 	if player.Health > player.HealthTotal {
   -1    59 		player.Health = player.HealthTotal
   -1    60 	}
   -1    61 	player.Send <- []Message{
   -1    62 		Message{
   -1    63 			"action":      "setHealth",
   -1    64 			"health":      player.Health,
   -1    65 			"healthTotal": player.HealthTotal,
   -1    66 		},
   -1    67 	}
   -1    68 }
   -1    69 
   -1    70 func (player *Player) AddItem(item string, amount uint) {
   -1    71 	value, ok := player.Inventory[item]
   -1    72 	if ok {
   -1    73 		value += amount
   -1    74 	} else {
   -1    75 		value = amount
   -1    76 	}
   -1    77 	player.Inventory[item] = value
   -1    78 
   -1    79 	player.Send <- []Message{
   -1    80 		Message{
   -1    81 			"action": "setInventory",
   -1    82 			"item":   item,
   -1    83 			"amount": value,
   -1    84 		},
   -1    85 	}
   -1    86 }
   -1    87 
   -1    88 func (player *Player) RemoveItem(item string, amount uint) {
   -1    89 	value, ok := player.Inventory[item]
   -1    90 	if !ok {
   -1    91 		value = 0
   -1    92 	} else if value <= amount {
   -1    93 		delete(player.Inventory, item)
   -1    94 		value = 0
   -1    95 	} else {
   -1    96 		value -= amount
   -1    97 		player.Inventory[item] = value
   -1    98 	}
   -1    99 
   -1   100 	player.Send <- []Message{
   -1   101 		Message{
   -1   102 			"action": "setInventory",
   -1   103 			"item":   item,
   -1   104 			"amount": value,
   -1   105 		},
   -1   106 	}
   -1   107 }
   -1   108 
   -1   109 func (player *Player) UseItem(item string) {
   -1   110 	// TODO: send result in a single transaction
   -1   111 	// TODO: check if item is in inventory
   -1   112 	if item == "potion" {
   -1   113 		if player.Health < player.HealthTotal {
   -1   114 			player.RemoveItem(item, 1)
   -1   115 			player.Heal(10)
   -1   116 		}
   -1   117 	}
   -1   118 }

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

@@ -103,6 +103,7 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
  103   103 		Speed:       20,
  104   104 		Health:      100,
  105   105 		HealthTotal: 100,
   -1   106 		Inventory:   make(map[string]uint),
  106   107 	}
  107   108 	conn.SetPongHandler(func(string) error {
  108   109 		player.alive = true

diff --git a/static/main.js b/static/main.js

@@ -317,15 +317,11 @@ socket.onmessage = function(event) {
  317   317             game.healthTotal = msg.healthTotal;
  318   318         } else if (msg.action === 'remove') {
  319   319             delete game.objects[msg.id];
  320    -1         } else if (msg.action === 'addItem') {
  321    -1             game.inventory[msg.item] = game.inventory[msg.item] || 0;
  322    -1             game.inventory[msg.item] += msg.amount;
  323    -1         } else if (msg.action === 'removeItem') {
  324    -1             if (msg.item in game.inventory) {
  325    -1                 game.inventory[msg.item] -= msg.amount;
  326    -1                 if (game.inventory[msg.item] <= 0) {
  327    -1                     delete game.inventory[msg.item];
  328    -1                 }
   -1   320         } else if (msg.action === 'setInventory') {
   -1   321             if (msg.amount) {
   -1   322                 game.inventory[msg.item] = msg.amount;
   -1   323             } else {
   -1   324                 delete game.inventory[msg.item];
  329   325             }
  330   326         } else {
  331   327             console.log(msg);