-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_jumpCallbackChecker.lua
88 lines (68 loc) · 3.51 KB
/
utils_jumpCallbackChecker.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- Name: utils_jumpCallbackChecker
-- Description: Script-side implemented custom callback launcher on jump initiated (Jump button pressed) and jump finished (ship appeared on new position)
--- Module API description:
--- * jumpCallbackChecker:init_ship(ship, jump_initiated_callback, jump_finished_callback) = Initialize checker for specified ship.
--- * jumpCallbackChecker:update() = Check all ships for jumps (should be called from scenario update() function.
require("utils.lua")
jumpCallbackChecker = {
checked_ships = {}, -- list of ships that have this functionality enabled
MIN_DISTANCE = 3000 -- minimum of 3U to trigger jump_finished_callback
}
-- -------------------------------------------------------------
-- Public API for the whole module
-- -------------------------------------------------------------
-- Initialize checker for specified ship
-- @param ship: PlayerSpaceship object
-- @param jump_initiated_callback: function that should be called when jump is initiated (countdown started), optional.
-- @param jump_finished_callback: function that should be called when jump is finished (ship moved), optional, but if set, requires jump_initiated_callback to be set.
function jumpCallbackChecker:init_ship(ship, jump_initiated_callback, jump_finished_callback)
local pos_x, pos_y = ship:getPosition()
local charge = ship:getJumpDriveCharge()
if jump_initiated_callback == nil then
jump_initiated_callback = function() end
end
if jump_finished_callback == nil then
jump_finished_callback = function() end
end
ship._jumpCallbackCheckerData = {
JUMP_CHARGE = charge,
LAST_POS_X = pos_x,
LAST_POS_Y = pos_y,
jump_initiated = jump_initiated_callback,
jump_finished = jump_finished_callback,
jump_in_progress = false
}
table.insert(jumpCallbackChecker.checked_ships, ship)
end
-- Check all ships if jump occured and update last values.
-- This function should be called in scenario update() function.
function jumpCallbackChecker:update()
for i, ship in ipairs(jumpCallbackChecker.checked_ships) do
jumpCallbackChecker:_update_ship(ship)
end
end
-- -------------------------------------------------------------
-- Private methods
-- -------------------------------------------------------------
-- Checks for jump callbacks trigger events and updates stored values on specified ship.
-- @param ship: Ship that should be checked if any of jump events occured on it.
function jumpCallbackChecker:_update_ship(ship)
if ship._jumpCallbackCheckerData ~= nil then
local charge = ship:getJumpDriveCharge()
if ship._jumpCallbackCheckerData.jump_in_progress then
dist = distance(ship._jumpCallbackCheckerData.LAST_POS_X, ship._jumpCallbackCheckerData.LAST_POS_Y, ship)
if dist > jumpCallbackChecker.MIN_DISTANCE then
ship._jumpCallbackCheckerData.jump_finished()
ship._jumpCallbackCheckerData.jump_in_progress = false
end
end
if charge < ship._jumpCallbackCheckerData.JUMP_CHARGE then
ship._jumpCallbackCheckerData.jump_initiated()
ship._jumpCallbackCheckerData.jump_in_progress = true
end
local pos_x, pos_y = ship:getPosition()
ship._jumpCallbackCheckerData.JUMP_CHARGE = ship:getJumpDriveCharge()
ship._jumpCallbackCheckerData.LAST_POS_X = pos_x
ship._jumpCallbackCheckerData.LAST_POS_Y = pos_y
end
end