Skip to content

Commit

Permalink
removing element from slice of pointers is not trivial; now picking u…
Browse files Browse the repository at this point in the history
…p works well, but game crashes while rendering
  • Loading branch information
VedVid committed Nov 16, 2018
1 parent d8a4e60 commit cf1fe76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ freely, subject to the following restrictions:

package main

import blt "bearlibterminal"
import (
blt "bearlibterminal"
)

func Controls(k int, p *Creature, b Board, c Creatures, o Objects) bool {
/* Function Controls is input handler.
Expand All @@ -41,7 +43,7 @@ func Controls(k int, p *Creature, b Board, c Creatures, o Objects) bool {
turnSpent = p.MoveOrAttack(-1, 0, b, c)

case blt.TK_G:
turnSpent = p.PickUp(o)
turnSpent = p.PickUp(&o)
}
return turnSpent
}
15 changes: 8 additions & 7 deletions monsters.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *Creature) Move(tx, ty int, b Board) bool {
return turnSpent
}

func (c *Creature) PickUp(o Objects) bool {
func (c *Creature) PickUp(o *Objects) bool {
/* PickUp is method that has *Creature as receiver
and slice of *Object as argument.
Creature tries to pick object up.
Expand All @@ -149,12 +149,13 @@ func (c *Creature) PickUp(o Objects) bool {
Picking objects up takes turn only if it's
successful attempt. */
turnSpent := false
for i, v := range o {
if v.X == c.X && v.Y == c.Y && v.Pickable == true {
item := o[i]
c.Inventory = append(c.Inventory, item)
o[i] = o[len(o)-1] // Doesn't it leaks a memory? \/
o = o[:len(o)-1] // It's slice of pointers to struct.
obj := *o
for i := 0; i <= len(obj); i++ {
if obj[i].X == c.X && obj[i].Y == c.Y && obj[i].Pickable == true {
c.Inventory = append(c.Inventory, obj[i])
copy(obj[i:], obj[i+1:])
obj[len(obj)-1] = nil
*o = obj[:len(obj)-1]
turnSpent = true
break
}
Expand Down

0 comments on commit cf1fe76

Please sign in to comment.