Skip to content

Instantly share code, notes, and snippets.

@a327ex
Created September 9, 2019 13:56
Show Gist options
  • Save a327ex/371fd375eba1de179cd6cd871f88df83 to your computer and use it in GitHub Desktop.
Save a327ex/371fd375eba1de179cd6cd871f88df83 to your computer and use it in GitHub Desktop.
export class Weapon
new: (@ref, @type="sword", index=1) =>
@wx, @wy, @wz, @wr, @wsx, @wsy = 0, 0, 0, 0, 1, 1
@name = @type .. tostring(index)
if @type == "sword"
@ox, @oy = 0, 0
@r = math.pi
@r1 = Spring -math.pi/2, 100, 10
@r2 = Spring 0, 100, 10
@swing = 1
elseif @type == "staff"
@ox, @oy = 0, 0
@r = math.pi/2
@r1 = Spring 0, 100, 10
@ox1 = Spring 0, 100, 10
@oy1 = Spring 0, 100, 10
update: (dt) =>
if @type == "sword"
@r1\update dt
@r2\update dt
if @swing == -1 then @r = @r1.x
if @swing == 1 then @r = @r2.x
elseif @type == "staff"
@r1\update dt
@ox1\update dt
@oy1\update dt
attack: =>
r = @ref.attack.ttr
if @type == "sword"
if @ref.__class == Player then camera\spring_shake 15, r
main.effects\add AnimatedEffect @ref.x, @ref.y, 0.025, "slash1", @ref, 48, -@swing
if @swing == 1 then
@swing = -@swing
@r1\pull -math.pi/4
else
@swing = -@swing
@r2\pull math.pi/4
@wsx, @wsy = 1.35, 1.35
@ref.timer\tween 0.1, @, {wsx: 1, wsy: 1}, Ease.linear, nil, "attack_scale"
elseif @type == "staff"
@r1\pull math.pi/4
@ox1\pull -48
@oy1\pull -32
if @ref.__class == Player then camera\spring_shake 8, r
x, y = math.rotate_scale_point @ref.x + 0 + @ox1.x, @ref.y - 32 - 24 + @oy1.x, r*math.angle_to_horizontal(r) + @r + @r1.x, 1, math.angle_to_horizontal(r), @ref.x, @ref.y - 28
@ref.attack.x = x
@ref.attack.y = y
elseif @type == "unarmed"
@wsx, @wsy = 1.35, 1.35
@ref.timer\tween 0.25, @, {wsx: 1, wsy: 1}, Ease.linear, nil, "attack_scale"
@wx, @wy = 64*math.cos(r), 64*math.sin(r)
@ref.timer\tween 0.4, @, {wx: 0, wy: 0}, Ease.cubic_in_out, nil, "attack_position"
if math.angle_to_vertical(r) == 1 then @wr = math.angle_to_horizontal(r)*math.pi/8
else @wr = math.angle_to_horizontal(r)*math.pi/16
@wz = rng\float 10, 16
@ref.timer\tween 0.4, @, {wr: 0, wz: 0}, Ease.linear, nil, "attack_rotation"
draw: =>
if @type == "sword"
gfx\push @ref.x - 1, @ref.y - 10, @ref.attack.ttr + @r
gfx\drawc @name, @ref.x - 16, @ref.y - 24, 0, 3.5, 3.5
gfx\pop!
elseif @type == "staff"
r = @ref.attack.ttr
gfx\push @ref.x, @ref.y - 24, r*math.angle_to_horizontal(r) + @r + @r1.x, 1, math.angle_to_horizontal(r)
gfx\drawc @name, @ref.x + 16 + @ox1.x, @ref.y - 16 - 24 + @oy1.x, 0, 3.5, 3.5
gfx\pop
draw_back: =>
r = @ref.attack.ttr
if @type == "sword"
rsc = 1 -- If aiming to the right, makes it so that the weapon is drawn behind the player when it swings up
if r <= math.pi/2 and r >= -math.pi/2 then rsc = -1
if @swing == -1*rsc then @\draw!
elseif @type == "staff"
if r <= -math.pi/6 and r >= -math.pi + math.pi/6 then @\draw!
draw_front: =>
r = @ref.attack.ttr
if @type == "sword"
rsc = 1
if r <= math.pi/2 and r >= -math.pi/2 then rsc = -1
if @swing == rsc then @\draw!
elseif @type == "staff"
if r > -math.pi/6 or r < -math.pi + math.pi/6 then @\draw!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment