func manageNpc(npc types.NPC) { eventChannel := events.Register(npc) go func() { defer events.Unregister(npc) for { event := <-eventChannel switch e := event.(type) { case events.TickEvent: if npc.GetRoaming() { room := model.GetRoom(npc.GetRoomId()) exits := room.GetExits() exitToTake := utils.Random(0, len(exits)-1) model.MoveCharacter(npc, exits[exitToTake]) } case events.CombatStartEvent: if npc == e.Defender { combat.StartFight(npc, nil, e.Attacker) } case events.CombatStopEvent: if npc == e.Defender { combat.StopFight(npc) } case events.DeathEvent: if npc == e.Character { model.DeleteCharacter(npc.GetId()) return } } } }() }
func specificNpcMenu(s *Session, npc types.NPC) { utils.ExecMenu(npc.GetName(), s, func(menu *utils.Menu) { menu.AddAction("r", "Rename", func() bool { name := getNpcName(s) if name != "" { npc.SetName(name) } return true }) menu.AddAction("d", "Delete", func() bool { model.DeleteCharacter(npc.GetId()) return false }) menu.AddAction("c", "Conversation", func() bool { conversation := npc.GetConversation() if conversation == "" { conversation = "<empty>" } s.WriteLine("Conversation: %s", conversation) newConversation := s.getRawUserInput("New conversation text: ") if newConversation != "" { npc.SetConversation(newConversation) } return true }) roamingState := "Off" if npc.GetRoaming() { roamingState = "On" } menu.AddAction("o", fmt.Sprintf("Roaming - %s", roamingState), func() bool { npc.SetRoaming(!npc.GetRoaming()) return true }) }) }