AttackComponent

Provide an interface that can reproduce the attack feature linking with HitComponent.

# Properties

##### inherited from Component:

[{"index":"boolean Enable (Sync)","0":"Checks whether Component is activated or not."}]

[{"index":"boolean EnableInHierarchy (ReadOnly) (HideFromInspector)","0":"Displays whether this Component is Enable in the hierarchy. If Entity Enable is false regardless of Component Enable, returns false."}]

[{"index":"Entity Entity (ReadOnly)","0":"The Entity that owns this component."}]

# Methods

[{"index":"table Attack(Shape shape, string attackInfo, CollisionGroup collisionGroup = nil)","0":"Calls the OnHit(Entity, Integer, boolean, string, int32) function of HitComponent within the shape area and triggers HitEvent. Returns all the HitComponents that are considered as attack targets.attackInfo is custom data that can be used as needed by the creator when implementing the attack directly. When utilizing this, the function must be overridden."}]

[{"index":"table Attack(Vector2 size, Vector2 offset, string attackInfo, CollisionGroup collisionGroup = nil)","0":"An attack function that can define the rectangular area. Size refers to the size of the rectangle. Offset is the entity rectangle's center point position."}]

[{"index":"void AttackFast(Shape shape, string attackInfo, CollisionGroup collisionGroup = nil)","0":"Performs an attack, no return value. You can improve the World's function by reducing unnecessary table entity creation."}]

[{"index":"table AttackFrom(Vector2 size, Vector2 position, string attackInfo, CollisionGroup collisionGroup = nil)","0":"An attack function that defines the rectangular area. Size refers to the size of the rectangle. Position is the entity rectangle's center point position based on the world coordinates."}]

[{"index":"boolean CalcCritical(Entity attacker, Entity defender, string attackInfo) (ScriptOverridable)","0":"Determines whether to launch a critical attack or not. A critical attack is not launched by default behavior, as it always returns false."}]

[{"index":"integer CalcDamage(Entity attacker, Entity defender, string attackInfo) (ScriptOverridable)","0":"Determines the damage value. Default behavior always returns 1."}]

[{"index":"float GetCriticalDamageRate() (ScriptOverridable)","0":"A critical attack determines how many times the default damage will be multiplied by. The default behavior always returns 2."}]

[{"index":"int32 GetDisplayHitCount(string attackInfo) (ScriptOverridable)","0":"Calculates how many hits to be displayed from one attack. Default behavior always returns 1."}]

[{"index":"boolean IsAttackTarget(Entity defender, string attackInfo) (ScriptOverridable)","0":"Determines whether the defender is a valid target for attack. When it returns false, it's removed from the Attack(), AttackFrom(), or AttackFast() functions. The default behavior returns false if the current state of the defender's StateComponent is 'DEAD' and both the defender and the opponent are players, while the opponent's PlayerComponent has a PVPMode property value as false."}]

[{"index":"void OnAttack(Entity defender) (ScriptOverridable)","0":"Entity attacks trigger this function."}]

##### inherited from Component:

[{"index":"boolean IsClient()","0":"Returns whether the current execution environment is a client or not."}]

[{"index":"boolean IsServer()","0":"Returns whether the current execution environment is a server or not."}]

# Events

[{"index":"AttackEvent","0":"This event occurs when an entity attacks. Occurs in AttackComponent."}]

# Examples

By extending AttackComponent, you can implement a custom attack method. This is an example of attacking monsters that enter a sqaure area.
    
    
    @Component
    script NormalAttackAbility extends Component
        @ExecSpace("ServerOnly")
        method void AttackNormal()
        local attackSize = Vector2(1,1)
        local playerController = self.Entity.PlayerControllerComponent
    
        if playerController ~= nil then
        	local attackOffset = Vector2(0.5 * playerController.LookDirectionX, 0.5)
        	self:Attack(attackSize, attackOffset, nil, CollisionGroups.Monster)
        end
        end
    
        override method integer CalcDamage(Entity attacker, Entity defender, string attackInfo)
        return 50
        end
    
        override method boolean CalcCritical(Entity attacker, Entity defender, string attackInfo)
        return _UtilLogic:RandomDouble() < 0.3
        end
    
        override method number GetCriticalDamageRate()
        return 2
        end
    
        @EventSender("Self")
        handler HandlePlayerActionEvent(PlayerActionEvent event)
        -- Parameters
        local ActionName = event.ActionName
        local PlayerEntity = event.PlayerEntity
        --------------------------------------------------------
        if self:IsClient() then return end
    
        if ActionName == "Attack" then
        	self:AttackNormal()
        end
        end
    end
    

# SeeAlso

  * HitComponent
  * PlayerControllerComponent
  * CollisionService
  * [Attack and Hit](/docs?postId=206)