Lua: Hijack Skill
This lua script sample is taken straight from Remnants. It is a special ability that allows a unit to take control of another unit in range.
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 | --------------------------------------------------------------------------- -- All Content © 2014 DigiPen (USA) Corporation, All Rights Reserved -- Filename : Skill_Hijack.lua -- Author(s) : Zhuhuii Yap -- Created : 12/DEC/2014 -- Description: Special ability that take control of another unit --------------------------------------------------------------------------- -- Get Bound Parameters for Current Action -- object - the object performing the action -- node - the action node of the current action being performed local this_object, node = GetArgs() -- Get varibles from action node -- target_id - the id of the unit we are trying to hijack -- range - the range of the ability local target_id = node:Get("TargetID") local range = node:Get("Range") -- Get required systems -- engine - the core engine -- factory - the system reponsible of object -- gamelogic - the system responsible for object's game logic -- transformHelper - the a helper libary for transformations local engine = GetEngine() local factory = engine:GetFactory() local gamelogic = engine:GetGameLogic() local transformHelper = engine:GetTransformHelper() -- Check for target validity if(fact:IsValid(target_id) == true) then -- Target is a vaild object -- Get target object and required components local target_object = factory:GetObject(target_id) local target_transform = target_object:GetTransformComponent() -- Get object (this_object) required component local this_transform = this_object:GetTransformComponent() -- Determine if target is in range and it is a unit local distance = transformHelper:Distance(target_transform, this_transform) if(distance < range and target_object:IsUnit() == true) then -- In range and target is a unit -- Get target stats component local target_stats = target_object:GetStats() -- Get object (this_object) stats component local this_stats = this_object:GetStats() -- Behavior of ability does not allow you hijack building only units if(target_stats:IsBuilding() == false) then -- Modifiy stats component target_stats.owner = this_stats.owner -- change owner -- Update game logic with the change in ownership of the object logic:UpdateRelevantOwners(target_object, this_stats.owner, target_stats.owner) -- Apply cooldown for successfully completed action local this_actionlist = this_object:GetActionList() this_actionlist:ApplyCooldown(node) end else -- Out of range or target is not a unit -- Attempt to move into range -- note: MoveIntoRange will requeue the action node when it is in range gamelogic:MoveIntoRange(obj, range, node) end end -- Terminate action, non-looping action node:SetComplete(obj) |