func saveQuit(a *area.Area, sav *save.Save) { err := sav.Save(*a) if err != nil { log.Println(err) } util.Quit() }
func askName() string { for { termbox.Flush() ev := termbox.PollEvent() // Listen only for keyboard events. if ev.Type != termbox.EventKey { continue } switch ev.Key { // Name entered. case termbox.KeyEnter: return name // Exit game. case ui.CancelKey: util.Quit() // Erase last character. case termbox.KeyBackspace2, termbox.KeyBackspace: backspace() continue // In termbox ev.Ch == 0x00 for ev.Key declared variables. So we need // this workaround. case termbox.KeySpace: addToName(0x20) continue } addToName(ev.Ch) } }
// HeroTurn listens on user input and then acts on it. func HeroTurn(sav *save.Save, a *area.Area) (int, state.State) { creature.Hero.DrawFOV(a) status.Update() ui.Hp(creature.Hero.Hp, creature.Hero.MaxHp) ui.Monsters(monsterInfo(creature.Hero.MonstersInRange(a))) termbox.Flush() // Listen for keystrokes. ev := termbox.PollEvent() if ev.Type != termbox.EventKey { return 0, state.Wilderness } switch ev.Ch { // case 'x': // sfx.Glitch() // return 0, state.Wilderness case '5', 's': // user wants to wait one turn. return creature.Hero.Speed, state.Wilderness case ui.PickUpItemKey: // user wants to pick up an item. return pickUp(a), state.Wilderness case ui.ShowInventoryKey: // user wants to look at inventory. return showInventory(a), state.Inventory // case ui.LookKey: // // user wants to look around. // return look(a), state.Look // case 'm': // // user wants to try debug function. // return debug(a), state.Wilderness case ui.DropItemKey: // user wants to drop an item. return dropItem(a), state.Drop // case ui.OpenDoorKey: // user wants to open a door. // return openDoor(a), state.Open // case ui.CloseDoorKey: // user wants to close a door. // return closeDoor(a), state.Close case ui.QuitKey: // user wants to quit game. util.Quit() case ui.SaveAndQuitKey: // user wants to save and exit. saveQuit(a, sav) } // user wants to move creature.Hero. return HeroMovement(ev, a), state.Wilderness }
func (attacker *Creature) damage(defender *Creature, a *area.Area) (s string) { lossOfHp := attacker.power() - defender.defense() if lossOfHp < 0 { lossOfHp = 0 } if defender.IsHero() { s = fmt.Sprintf("You take %d damage from %s!", lossOfHp, attacker.Name()) } else if attacker.IsHero() { s = fmt.Sprintf("You inflict %d damage to %s!", lossOfHp, defender.Name()) } else { s = fmt.Sprintf("%s takes %d damage from %s!", strings.Title(defender.Name()), lossOfHp, attacker.Name()) } defender.Hp -= lossOfHp if defender.Hp <= 0 { if defender.IsHero() { Hero.DrawFOV(a) status.Println(s, termbox.ColorWhite) status.Println("You die. Press any key to quit.", termbox.ColorWhite) status.Update() termbox.Flush() termbox.PollEvent() util.Quit() } else if attacker.IsHero() { s += fmt.Sprintf(" You killed %s!", defender.Name()) } a.Monsters[coord.Coord{defender.X(), defender.Y()}] = nil _, ok := a.Items[defender.Coord()] if !ok { a.Items[defender.Coord()] = new(area.Stack) } for _, i := range defender.Inventory { a.Items[defender.Coord()].Push(i) } a.Items[defender.Coord()].Push(defender.Corpse()) } return s }