Example #1
0
// load parses a JSON data file into a go creature object.
func load(filename string) (c *Creature, err error) {
	// jsonCreature is a temporary struct for easier conversion between JSON and
	// go structs.
	type jsonCreature struct {
		Name     string
		Graphics struct {
			Ch string
			Fg map[string]string
			Bg map[string]string
		}
		Hp           int
		Strength     int
		Sight        int
		Speed        int
		Regeneration int
	}

	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, errutil.Err(err)
	}

	var jc jsonCreature
	err = json.Unmarshal(buf, &jc)
	if err != nil {
		return nil, errutil.Err(err)
	}

	fg, err := util.ParseColor(jc.Graphics.Fg)
	if err != nil {
		return nil, errutil.Err(err)
	}
	bg, err := util.ParseColor(jc.Graphics.Bg)
	if err != nil {
		return nil, errutil.Err(err)
	}

	c = &Creature{
		name:         jc.Name,
		Hp:           jc.Hp,
		MaxHp:        jc.Hp,
		Strength:     jc.Strength,
		Speed:        jc.Speed,
		Sight:        jc.Sight,
		Regeneration: jc.Regeneration,
		Inventory:    make(Inventory, len(item.Positions)),
	}
	c.SetPathable(false)
	c.SetGraphics(termbox.Cell{
		Ch: rune(jc.Graphics.Ch[0]),
		Fg: fg,
		Bg: bg,
	})
	return c, nil
}
Example #2
0
// load parses a JSON data file into a go terrain object.
func load(filename string) (t *Terrain, err error) {
	// jsonTerrain is a temporary struct for easier conversion between JSON and
	// go structs.
	type jsonTerrain struct {
		Name     string
		Graphics struct {
			Ch string
			Fg map[string]string
			Bg map[string]string
		}
		Pathable bool
	}

	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, errutil.Err(err)
	}

	var jt jsonTerrain
	err = json.Unmarshal(buf, &jt)
	if err != nil {
		return nil, errutil.Err(err)
	}

	fg, err := util.ParseColor(jt.Graphics.Fg)
	if err != nil {
		return nil, errutil.Err(err)
	}
	bg, err := util.ParseColor(jt.Graphics.Bg)
	if err != nil {
		return nil, errutil.Err(err)
	}

	t = &Terrain{
		name: jt.Name,
	}
	t.SetPathable(jt.Pathable)
	t.SetGraphics(termbox.Cell{
		Ch: rune(jt.Graphics.Ch[0]),
		Fg: fg,
		Bg: bg,
	})
	return t, nil
}
Example #3
0
// load parses a JSON data file into a go object.
func load(filename string) (o *Object, err error) {
	// jsonobject is a temporary struct for easier conversion between JSON and
	// go structs.
	type jsonobject struct {
		Name     string
		Graphics struct {
			Ch string
			Fg map[string]string
			Bg map[string]string
		}
		Pathable bool
	}

	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, errutil.Err(err)
	}

	var jo jsonobject
	err = json.Unmarshal(buf, &jo)
	if err != nil {
		return nil, errutil.Err(err)
	}

	fg, err := util.ParseColor(jo.Graphics.Fg)
	if err != nil {
		return nil, errutil.Err(err)
	}
	bg, err := util.ParseColor(jo.Graphics.Bg)
	if err != nil {
		return nil, errutil.Err(err)
	}

	o = &Object{
		name: jo.Name,
	}
	o.SetPathable(jo.Pathable)
	o.SetGraphics(termbox.Cell{
		Ch: rune(jo.Graphics.Ch[0]),
		Fg: fg,
		Bg: bg,
	})
	return o, nil
}
Example #4
0
// load parses a JSON data file into a go item object.
func load(filename string) (i DrawItemer, err error) {
	// jsonItem is a temporary struct for easier conversion between JSON and
	// go structs.
	type jsonItem struct {
		Name     string
		Category string
		Flavor   string
		Use      string
		Rarity   string
		Num      int
		Graphics struct {
			Ch string
			Fg map[string]string
			Bg map[string]string
		}
		Effects []struct {
			Type      string
			Magnitude int
		}
		Pathable bool
	}

	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, errutil.Err(err)
	}

	var ji jsonItem
	err = json.Unmarshal(buf, &ji)
	if err != nil {
		return nil, errutil.Err(err)
	}

	fg, err := util.ParseColor(ji.Graphics.Fg)
	if err != nil {
		return nil, errutil.Err(err)
	}
	bg, err := util.ParseColor(ji.Graphics.Bg)
	if err != nil {
		return nil, errutil.Err(err)
	}

	rarity, err := parseRarity(ji.Rarity)
	if err != nil {
		return nil, errutil.Err(err)
	}

	effs, err := parseEffects(ji.Effects)
	if err != nil {
		return nil, errutil.Err(err)
	}
	log.Printf("%#v\n", effs)
	j := Item{
		name:     ji.Name,
		flavor:   ji.Flavor,
		use:      ji.Use,
		rarity:   rarity,
		count:    ji.Num,
		category: ji.Category,
		effects:  effs,
	}
	j.SetPathable(ji.Pathable)
	j.SetGraphics(termbox.Cell{
		Ch: rune(ji.Graphics.Ch[0]),
		Fg: fg,
		Bg: bg,
	})
	switch ji.Category {
	case "weapon":
		i = &Weapon{Item: j}
	case "potion":
		i = &Potion{Item: j}
	case "tool":
		i = &Tool{Item: j}
	case "ring":
		i = &Ring{Item: j}
	case "corpse":
		i = &Corpse{Item: j}
	case "amulet":
		i = &Amulet{Item: j}
	case "gold":
		i = &Gold{Item: j}
	case "scroll":
		i = &Scroll{Item: j}
	default:
		log.Fatalln("implement %s", ji.Category)
	}

	return i, nil
}