Пример #1
0
func (self *Session) handleAction(action string, arg string) {
	if arg == "" {
		direction := types.StringToDirection(action)

		if direction != types.DirectionNone {
			if self.GetRoom().HasExit(direction) {
				err := model.MoveCharacter(self.pc, direction)
				if err == nil {
					self.PrintRoom()
				} else {
					self.printError(err.Error())
				}

			} else {
				self.printError("You can't go that way")
			}

			return
		}
	}

	handler, found := actions[action]

	if found {
		if handler.alias != "" {
			handler = actions[handler.alias]
		}
		handler.exec(self, arg)
	} else {
		self.printError("You can't do that")
	}
}
Пример #2
0
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
				}
			}
		}
	}()
}