Sha256: 6d2c6b8bd696388ad1b4aae92c4f13857cdfb5cf1b5be96f54ff4452a7b71360

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

module Example
  class NpcGroup < GameMachine::Actor::Base
    include GameMachine::Commands

    attr :npcs
    def post_init(*args)
      @npcs = {}
      group = args.first
      klass = args.last
      group.each do |npc_id|
        npcs[npc_id] = klass.new(npc_id)
      end

      # schedule_once was not yet in the base actor, needs to be abstracted
      # out to there
      unit = java.util.concurrent.TimeUnit::MILLISECONDS
      @duration = GameMachine::JavaLib::Duration.create(30, unit)
      @scheduler = get_context.system.scheduler
      @dispatcher = get_context.system.dispatcher
      schedule_once('update')
    end

    def schedule_once(message)
      @scheduler.schedule_once(@duration, get_self, message, @dispatcher, nil)
    end

    def on_receive(message)
      if message == 'update'
        npcs.each_value do |npc|
          npc.update
        end
        schedule_once('update')
      elsif message.is_a?(Models::CombatUpdate)
        if npc = npcs.fetch(message.target,nil)
          npc.update_combat(message)
        end
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
game_machine-1.0.4 games/example/lib/npc_group.rb
game_machine-1.0.2 games/example/lib/npc_group.rb
game_machine-0.0.11 games/example/lib/npc_group.rb
game_machine-0.0.10 games/example/lib/npc_group.rb
game_machine-0.0.9 games/example/lib/npc_group.rb
game_machine-0.0.8 games/example/lib/npc_group.rb