Module: Scheduler::EventTasks

Defined in:
scripts/00700 Ajout_PSDK/00101 EventTasks.rb

Overview

Module that aim to add task triggered by events actions

List of the event actions :

- begin_step
- begin_jump
- begin_slide
- end_step
- end_jump
- end_slide

Events can be specified with the following criteria

- map_id / :any : ID of the map where the task can trigger
- event_id / :any : ID of the event that trigger the task (-1 = player, -2 its first follower, -3 its second, ...)

Parameter sent to the task :

- event : Game_Character object that triggered the task
- event_id : ID of the event that triggered the task (for :any tasks)
- map_id : ID of the map where the task was triggered (for :any tasks)

Important note : The system will detect the original id & map of the events (that's why the event object is sent & its id)

Class Method Summary collapse

Class Method Details

.delete(task_type, description, event_id, map_id)

Remove a task

Parameters:

  • task_type (Symbol)

    one of the specific tasks

  • description (String)

    description allowing to retrieve the task

  • event_id (Integer, :any)

    id of the event that triggers the task

  • map_id (Integer, :any)

    id of the map where the task triggers



100
101
102
103
104
105
# File 'scripts/00700 Ajout_PSDK/00101 EventTasks.rb', line 100

def delete(task_type, description, event_id, map_id)
  return unless (tasks = @tasks[task_type])
  return unless (tasks = tasks[map_id])
  return unless (tasks = tasks[event_id])
  tasks.delete(description)
end

.on(task_type, description, event_id = :any, map_id = :any, &task)

Add a new task

Parameters:

  • task_type (Symbol)

    one of the specific tasks

  • description (String)

    description allowing to retrieve the task

  • event_id (Integer, :any) (defaults to: :any)

    id of the event that triggers the task

  • map_id (Integer, :any) (defaults to: :any)

    id of the map where the task triggers

  • task (Proc)

    task executed



34
35
36
37
38
39
# File 'scripts/00700 Ajout_PSDK/00101 EventTasks.rb', line 34

def on(task_type, description, event_id = :any, map_id = :any, &task)
  tasks = (@tasks[task_type] ||= {})
  tasks = (tasks[map_id] ||= {})
  tasks = (tasks[event_id] ||= {})
  tasks[description] = task
end

.resolve_id(event) ⇒ Integer

Resolve the id of the event

Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'scripts/00700 Ajout_PSDK/00101 EventTasks.rb', line 71

def resolve_id(event)
  if event.is_a?(Game_Event)
    return event.original_id
  elsif event == $game_player
    return -1
  end
  # Follower resolution
  id = -1
  follower = $game_player
  while (follower = follower.follower)
    id -= 1
    return id if follower == event
  end
  return 0
end

.resolve_map_id(event) ⇒ Integer

Resolve the id of the event

Parameters:

Returns:



90
91
92
93
# File 'scripts/00700 Ajout_PSDK/00101 EventTasks.rb', line 90

def resolve_map_id(event)
  return event.original_map if event.is_a?(Game_Event)
  return $game_map.map_id
end

.trigger(task_type, event)

Trigger a specific task

Parameters:

  • task_type (Symbol)

    one of the specific tasks

  • event (Game_Character)

    event triggering the task



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'scripts/00700 Ajout_PSDK/00101 EventTasks.rb', line 44

def trigger(task_type, event)
  return unless (tasks = @tasks[task_type])
  event_id = resolve_id(event)
  map_id = resolve_map_id(event)
  # Tasks of the current map
  if (map_tasks = tasks[map_id])
    if (event_tasks = map_tasks[event_id])
      event_tasks.each_value { |task| task.call(event, event_id, map_id) }
    end
    if (event_tasks = map_tasks[:any])
      event_tasks.each_value { |task| task.call(event, event_id, map_id) }
    end
  end
  # Tasks of all the map
  if (map_tasks = tasks[:any])
    if (event_tasks = map_tasks[event_id])
      event_tasks.each_value { |task| task.call(event, event_id, map_id) }
    end
    if (event_tasks = map_tasks[:any])
      event_tasks.each_value { |task| task.call(event, event_id, map_id) }
    end
  end
end