Skip to content

Commit

Permalink
Polish area attack dragger code and visuals (FAForever#6317)
Browse files Browse the repository at this point in the history
- Clean up area attack script with removing unused values and
refactoring variable names.
- Make the area attack radial dragger red colored.
- Make the area attack dragger show up in the worldview it is clicked on
(previously it only worked in one worldview).
  • Loading branch information
lL1l1 authored Jul 6, 2024
1 parent c8b1115 commit 3e4d0af
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 73 deletions.
2 changes: 1 addition & 1 deletion changelog/snippets/features.6095.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- (#6095, #6312) Implement a new area attack order when you left click and drag while issuing an attack command. It issues extra orders within the radius of the command which you can then distribute to spread out the attacks.
- (#6095, #6312, #6317) Implement a new area attack order when you left click and drag while issuing an attack command. It issues extra orders within the radius of the command which you can then distribute to spread out the attacks.
2 changes: 1 addition & 1 deletion lua/SimCallbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ do

local start = GetSystemTimeSecondsOnlyForProfileUse()
import("/lua/sim/commands/area-attack-ground-order.lua").AreaAttackOrder(selection,
{ lastCommand.x, lastCommand.y, lastCommand.z }, true, data.Radius)
{ lastCommand.x, lastCommand.y, lastCommand.z }, data.Radius)
SPEW("Time taken for area attack order: ", 1000 * (GetSystemTimeSecondsOnlyForProfileUse() - start), "miliseconds")
end

Expand Down
24 changes: 0 additions & 24 deletions lua/shared/commands/area-attack-order.lua

This file was deleted.

13 changes: 3 additions & 10 deletions lua/sim/commands/area-attack-ground-order.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@

-- upvalue scope for performance
local TableGetn = table.getn
local TableSort = table.sort
local TableSetn = table.setn
local TableInsert = table.insert
local TableRemove = table.remove

local StringFormat = string.format

local IssueReclaim = IssueReclaim
local IssueAttack = IssueAttack

local MathRound = math.round
local MathSqrt = math.sqrt
Expand All @@ -51,9 +45,8 @@ end

---@param units Unit[]
---@param target Vector
---@param doPrint boolean # if true, prints information about the order
---@param radius? number
function AreaAttackOrder(units, target, doPrint, radius)
---@param radius number
function AreaAttackOrder(units, target, radius)
local unitCount = TableGetn(units)
if unitCount == 0 then
return
Expand Down
35 changes: 11 additions & 24 deletions lua/ui/controls/draggers/radial.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,37 @@ local UIRenderableCircle = import('/lua/ui/game/renderable/circle.lua').UIRender
local Dragger = import('/lua/maui/dragger.lua').Dragger
local DraggerInit = Dragger.__init

local TrashAdd = TrashBag.Add

---@class UIRadialDragger : Dragger
---@field Origin Vector
---@field Destination Vector
---@field ShapeStart UIRenderableCircle
---@field RadiusCircle UIRenderableCircle
---@field MinimumDistance number
---@field MaximumDistance number
---@field Width number
---@field WorldView WorldView
---@field Callback fun(origin: Vector, radius: number)
RadialDragger = Class(Dragger) {

Size = 0.2,
Thickness = 0.05,
Color = 'ffffff',

---@param self UIRadialDragger
---@param view WorldView
---@param callback fun(origin: Vector, radius: number)
---@param keycode 'LBUTTON' | 'MBUTTON' | 'RBUTTON'
---@param maximumWidth number
---@param minimumDistance number
---@param maximumDistance number
__init = function(self, view, callback, keycode, maximumWidth, minimumDistance, maximumDistance)
__init = function(self, view, callback, keycode, minimumDistance)
DraggerInit(self)

-- store parameters
self.Width = maximumWidth
self.MinimumDistance = minimumDistance
self.MaximumDistance = maximumDistance
self.WorldView = view
self.Callback = callback

-- prepare visuals
local mouseWorldPosition = GetMouseWorldPos()

local size = self.Size
local trash = self.Trash
local thickness = self.Thickness
self.ShapeStart = trash:Add(UIRenderableCircle(view, 'rectangle-dragger-start', mouseWorldPosition[1],
mouseWorldPosition[2], mouseWorldPosition[3], size, 'ffffff', thickness))
-- default 0.2 radius, `'ffffff'` (White) color, and 0.05 minimum thickness
self.RadiusCircle = TrashAdd(self.Trash, UIRenderableCircle(view, 'radialDragger' .. keycode, mouseWorldPosition[1],
mouseWorldPosition[2], mouseWorldPosition[3], 0.2, 'ffffff', 0.05))

if minimumDistance > 0 then
self.ShapeStart:Hide()
self.RadiusCircle:Hide()
end

self.Origin = mouseWorldPosition
Expand All @@ -81,7 +69,6 @@ RadialDragger = Class(Dragger) {
---@param x number # x coordinate of screen position
---@param y number # y coordinate of screen position
OnMove = function(self, x, y)
local width = self.Width
local view = self.WorldView

local ps = self.Origin
Expand All @@ -92,11 +79,11 @@ RadialDragger = Class(Dragger) {
local distance = math.sqrt(dx * dx + dz * dz)

if distance > self.MinimumDistance then
self.ShapeStart:Show()
self.ShapeStart.Size = distance
self.RadiusCircle:Show()
self.RadiusCircle.Size = distance
else
-- try to hide it
self.ShapeStart:Hide()
self.RadiusCircle:Hide()
end
end,

Expand Down
28 changes: 15 additions & 13 deletions lua/ui/game/hotkeys/area-attack-order.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
--** SOFTWARE.
--******************************************************************************************************

local GetTopmostWorldViewAt = import("/lua/ui/game/worldview.lua").GetTopmostWorldViewAt

local RadialDragger = import("/lua/ui/controls/draggers/radial.lua").RadialDragger
local MaximumWidth = import("/lua/shared/commands/area-reclaim-order.lua").MaximumWidth
local MaximumDistance = import("/lua/shared/commands/area-reclaim-order.lua").MaximumDistance

---@type number
local MinimumDistance = 4
local MinimumRadius = 4

---@param value number
SetMinimumDistance = function(value)
if type(value) != 'number' then
error('Expected a number, got ' .. type(value))
end

MinimumDistance = value
MinimumRadius = value
end

---@type Keycode
Expand All @@ -51,8 +51,8 @@ end

---@param origin Vector
---@param radius number
local AreaReclaimOrderCallback = function(origin, radius)
if radius < MinimumDistance then
local AreaAttackOrderCallback = function(origin, radius)
if radius < MinimumRadius then
return
end

Expand All @@ -61,16 +61,18 @@ end

---@param command UserCommand
AreaAttackOrder = function(command)

local mousePos = GetMouseScreenPos()
local worldView = GetTopmostWorldViewAt(mousePos[1], mousePos[2])

local worldView = import("/lua/ui/game/worldview.lua").viewLeft

RadialDragger(
local radialDragger = RadialDragger(
worldView,
AreaReclaimOrderCallback,
AreaAttackOrderCallback,
DragKeycode,
MinimumDistance,
MaximumWidth,
MaximumDistance
MinimumRadius
)

radialDragger.RadiusCircle.Color = 'e52c2c'
radialDragger.RadiusCircle.Thickness = 0.03

end

0 comments on commit 3e4d0af

Please sign in to comment.