- commit
- 7a06c482744ac70993ff0a2b43c2309ef739bcbd
- parent
- 44febec7a7107997f7df7d9d4731b5e685eb5ba3
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-10-05 06:36
add more items
Diffstat
| M | game.go | 35 | +++++++---------------------------- |
| A | items.go | 109 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | monster.go | 2 | +- |
| M | player.go | 87 | +++++++++++++++++++++++++++++++++++++++++++++++-------------- |
| M | static/main.js | 4 | ++-- |
5 files changed, 186 insertions, 51 deletions
diff --git a/game.go b/game.go
@@ -218,34 +218,13 @@ func (game *Game) run() {
218 218 "id": player.Id,
219 219 })
220 220 player.Enqueue(Message{
221 -1 "action": "setStat",
222 -1 "stat": "health",
223 -1 "value": player.Health,
224 -1 })
225 -1 player.Enqueue(Message{
226 -1 "action": "setStat",
227 -1 "stat": "healthTotal",
228 -1 "value": player.HealthTotal,
229 -1 })
230 -1 player.Enqueue(Message{
231 -1 "action": "setStat",
232 -1 "stat": "attack",
233 -1 "value": player.Attack,
234 -1 })
235 -1 player.Enqueue(Message{
236 -1 "action": "setStat",
237 -1 "stat": "defense",
238 -1 "value": player.Defense,
239 -1 })
240 -1 player.Enqueue(Message{
241 -1 "action": "setStat",
242 -1 "stat": "lineOfSight",
243 -1 "value": player.LineOfSight,
244 -1 })
245 -1 player.Enqueue(Message{
246 -1 "action": "setStat",
247 -1 "stat": "speed",
248 -1 "value": player.Speed,
-1 221 "action": "setStats",
-1 222 "health": player.Health,
-1 223 "healthTotal": player.HealthTotal,
-1 224 "attack": player.Attack,
-1 225 "defense": player.Defense,
-1 226 "lineOfSight": player.LineOfSight,
-1 227 "speed": player.Speed,
249 228 })
250 229 player.Enqueue(Message{
251 230 "action": "setLevel",
diff --git a/items.go b/items.go
@@ -0,0 +1,109 @@
-1 1 package main
-1 2
-1 3 const (
-1 4 CONSUMABLE uint = 1
-1 5 WEAPON = 2
-1 6 ARMOR = 3
-1 7 )
-1 8
-1 9 type Item struct {
-1 10 Type uint
-1 11 Health uint
-1 12 HealthTotal uint
-1 13 Attack uint
-1 14 Defense uint
-1 15 LineOfSight int
-1 16 Speed int
-1 17 }
-1 18
-1 19 var Items = map[string]Item{
-1 20 // consumables
-1 21 "Small Potion": Item{
-1 22 Type: CONSUMABLE,
-1 23 Health: 10,
-1 24 },
-1 25 "Potion": Item{
-1 26 Type: CONSUMABLE,
-1 27 Health: 25,
-1 28 },
-1 29 "Great Potion": Item{
-1 30 Type: CONSUMABLE,
-1 31 Health: 100,
-1 32 },
-1 33 "Small Life Elixir": Item{
-1 34 Type: CONSUMABLE,
-1 35 HealthTotal: 1,
-1 36 },
-1 37 "Life Elixir": Item{
-1 38 Type: CONSUMABLE,
-1 39 HealthTotal: 5,
-1 40 },
-1 41 "Great Life Elixir": Item{
-1 42 Type: CONSUMABLE,
-1 43 HealthTotal: 20,
-1 44 },
-1 45
-1 46 // weapons
-1 47 "Butterknive": {
-1 48 Type: WEAPON,
-1 49 Attack: 1,
-1 50 },
-1 51 "Sword": {
-1 52 Type: WEAPON,
-1 53 Attack: 3,
-1 54 },
-1 55 "Battleaxe": Item{
-1 56 Type: WEAPON,
-1 57 Attack: 4,
-1 58 Speed: -5,
-1 59 },
-1 60 "Daggers": Item{
-1 61 Type: WEAPON,
-1 62 Attack: 2,
-1 63 Speed: 5,
-1 64 },
-1 65 "Sting": Item{
-1 66 Type: WEAPON,
-1 67 Attack: 2,
-1 68 LineOfSight: 2,
-1 69 },
-1 70 "Shield": Item{
-1 71 Type: WEAPON,
-1 72 Defense: 3,
-1 73 },
-1 74
-1 75 // armor
-1 76 "Leather Armor": Item{
-1 77 Type: ARMOR,
-1 78 Defense: 2,
-1 79 Speed: -5,
-1 80 },
-1 81 "Shining Armor": Item{
-1 82 Type: ARMOR,
-1 83 Defense: 2,
-1 84 LineOfSight: 3,
-1 85 Speed: -5,
-1 86 },
-1 87 "Heavy Armor": Item{
-1 88 Type: ARMOR,
-1 89 Defense: 3,
-1 90 Speed: -10,
-1 91 },
-1 92 "Spiked Armor": Item{
-1 93 Type: ARMOR,
-1 94 Attack: 1,
-1 95 Defense: 2,
-1 96 Speed: -10,
-1 97 },
-1 98 "Cloak": Item{
-1 99 Type: ARMOR,
-1 100 Defense: 1,
-1 101 LineOfSight: 1,
-1 102 Speed: 5,
-1 103 },
-1 104 "Body Oil": Item{
-1 105 Type: ARMOR,
-1 106 Attack: 1,
-1 107 Speed: 10,
-1 108 },
-1 109 }
diff --git a/monster.go b/monster.go
@@ -77,7 +77,7 @@ func (monster *Monster) TakeDamage(amount uint) {
77 77 if amount > monster.Health {
78 78 monster.quit <- true
79 79 delete(monster.Game.Monsters, monster)
80 -1 monster.Game.addToPile(monster.Pos, "potion", 1)
-1 80 monster.Game.addToPile(monster.Pos, "Small Potion", 1)
81 81 monster.Game.Enqueue(Message{
82 82 "action": "remove",
83 83 "id": monster.Id,
diff --git a/player.go b/player.go
@@ -18,6 +18,8 @@ type Player struct {
18 18 LineOfSight uint
19 19 Speed uint
20 20 Inventory map[string]uint
-1 21 Weapon string
-1 22 Armor string
21 23 }
22 24
23 25 type PlayerMessage struct {
@@ -41,26 +43,40 @@ func (player *Player) TakeDamage(amount uint) {
41 43 player.quit <- true
42 44 } else {
43 45 player.Health -= amount
44 -1 player.Enqueue(Message{
45 -1 "action": "setStat",
46 -1 "stat": "health",
47 -1 "value": player.Health,
48 -1 })
-1 46 player.AnnounceStats()
49 47 }
50 48 }
51 49
52 -1 func (player *Player) Heal(amount uint) {
53 -1 player.Health += amount
54 -1 if player.Health > player.HealthTotal {
55 -1 player.Health = player.HealthTotal
56 -1 }
-1 50 func (player *Player) AnnounceStats() {
57 51 player.Enqueue(Message{
58 -1 "action": "setStat",
59 -1 "stat": "health",
60 -1 "value": player.Health,
-1 52 "action": "setStats",
-1 53 "health": player.Health,
-1 54 "healthTotal": player.HealthTotal,
-1 55 "attack": player.Attack,
-1 56 "defense": player.Defense,
-1 57 "lineOfSight": player.LineOfSight,
-1 58 "speed": player.Speed,
61 59 })
62 60 }
63 61
-1 62 func (player *Player) ApplyItem(item Item) {
-1 63 player.Health += item.Health + item.HealthTotal
-1 64 player.HealthTotal += item.HealthTotal
-1 65 player.Attack += item.Attack
-1 66 player.Defense += item.Defense
-1 67 player.LineOfSight = uint(int(player.LineOfSight) + item.LineOfSight)
-1 68 player.Speed = uint(int(player.Speed) + item.Speed)
-1 69 }
-1 70
-1 71 func (player *Player) UnapplyItem(item Item) {
-1 72 player.Health -= item.Health
-1 73 player.HealthTotal -= item.HealthTotal
-1 74 player.Attack -= item.Attack
-1 75 player.Defense -= item.Defense
-1 76 player.LineOfSight = uint(int(player.LineOfSight) - item.LineOfSight)
-1 77 player.Speed = uint(int(player.Speed) - item.Speed)
-1 78 }
-1 79
64 80 func (player *Player) AddItem(item string, amount uint) {
65 81 value, ok := player.Inventory[item]
66 82 if ok {
@@ -136,16 +152,47 @@ func (player *Player) DropItem(item string) {
136 152 }
137 153 }
138 154
139 -1 func (player *Player) UseItem(item string) {
140 -1 if _, ok := player.Inventory[item]; !ok {
-1 155 func (player *Player) UseItem(name string) {
-1 156 if _, ok := player.Inventory[name]; !ok {
141 157 return
142 158 }
143 159
144 -1 switch item {
145 -1 case "potion":
146 -1 if player.Health < player.HealthTotal {
147 -1 player.RemoveItem(item)
148 -1 player.Heal(10)
-1 160 item, ok := Items[name]
-1 161 if !ok {
-1 162 return
-1 163 }
-1 164
-1 165 if item.Health != 0 &&
-1 166 item.HealthTotal == 0 &&
-1 167 item.Attack == 0 &&
-1 168 item.Defense == 0 &&
-1 169 item.LineOfSight == 0 &&
-1 170 item.Speed == 0 &&
-1 171 player.Health == player.HealthTotal {
-1 172 return
-1 173 }
-1 174
-1 175 switch item.Type {
-1 176 case CONSUMABLE:
-1 177 player.RemoveItem(name)
-1 178 player.ApplyItem(item)
-1 179 case WEAPON:
-1 180 if old, ok := Items[player.Weapon]; ok {
-1 181 player.UnapplyItem(old)
-1 182 }
-1 183 player.ApplyItem(item)
-1 184 player.Weapon = name
-1 185 case ARMOR:
-1 186 if old, ok := Items[player.Armor]; ok {
-1 187 player.UnapplyItem(old)
149 188 }
-1 189 player.ApplyItem(item)
-1 190 player.Armor = name
150 191 }
-1 192
-1 193 if player.Health > player.HealthTotal {
-1 194 player.Health = player.HealthTotal
-1 195 }
-1 196
-1 197 player.AnnounceStats()
151 198 }
diff --git a/static/main.js b/static/main.js
@@ -363,8 +363,8 @@ socket.onmessage = function(event) {
363 363 if (game.objects[msg.id].type === 'player') {
364 364 game.updateSeen(msg.pos);
365 365 }
366 -1 } else if (msg.action === 'setStat') {
367 -1 game.stats[msg.stat] = msg.value;
-1 366 } else if (msg.action === 'setStats') {
-1 367 game.stats = msg;
368 368 } else if (msg.action === 'remove') {
369 369 delete game.objects[msg.id];
370 370 } else if (msg.action === 'setInventory') {