Skip to content

Instantly share code, notes, and snippets.

@a327ex
Created September 9, 2019 13:56
Show Gist options
  • Save a327ex/a9c46123bd4d64edb35e1e3345075a49 to your computer and use it in GitHub Desktop.
Save a327ex/a9c46123bd4d64edb35e1e3345075a49 to your computer and use it in GitHub Desktop.
export class Attack
new: (@ref, @name, @ts, @tspolicy="closest") =>
@x, @y = 0, 0
@r = 0
@ttr = 0
@timer = 0
@in_range_timer = 0
@\set @name
update: (dt) =>
@ttr = @ref.r
target = nil
if @tspolicy == "closest"
closest_locals = {}
for t in *@ts
if objects = main.objects\get_objects_by_class t
if closest_local = @ref\get_closest_object objects
table.insert closest_locals, closest_local
if target = @ref\get_closest_object closest_locals
@ttr = math.angle @ref.x, @ref.y, target.x, target.y
@timer += dt
if @timer > @cd
@can_attack = true
if target
if math.distance(@ref.x, @ref.y, target.x, target.y) < @range
if @precd
@in_range_timer += dt
if @in_range_timer > @precd and @can_attack
@in_range_timer = 0
@\attack!
elseif @can_attack
@\attack!
else
@in_range_timer = 0
attack: =>
@can_attack = false
@timer = 0
@ref.weapon\attack!
switch @name
when "tackle_enemy"
@ref\query_rectangle 140, 100, 48, @ttr, @ts, (obj)-> obj\hit rng\int(3, 10), @ref.x + 72*math.cos(@ttr), @ref.y + 72*math.sin(@ttr), rng\float(80, 120), @ttr
when "sword_slash"
@ref\query_rectangle 260, 140, 48, @ttr, @ts, (obj)-> obj\hit rng\int(13, 20), @ref.x + 56*math.cos(@ttr), @ref.y + 56*math.sin(@ttr), rng\float(80, 120), @ttr
set: (@name) =>
@cd, @range, @precd = nil, nil, nil
for k, v in pairs attacks[@name]
switch k
when "cd"
@cd = v
when "range"
@range = v
when "precd"
@precd = v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment