Skip to content

Instantly share code, notes, and snippets.

@arachonteur
Created February 12, 2018 16:17
Show Gist options
  • Save arachonteur/dbd7ce0c5b396472c6478699c325bd33 to your computer and use it in GitHub Desktop.
Save arachonteur/dbd7ce0c5b396472c6478699c325bd33 to your computer and use it in GitHub Desktop.
A quick bullet-velocity demo for my good friend Jade
-- title: shoot test
-- author: vriska done it
-- desc: hope this helps
-- script: lua
-- input: mouse
t=0
entities = {}
-- This function returns a 2d vector
-- 8ased on X, Y and the angle.
-- Using it with velocities results
-- in it being offset by 45 degrees
-- and I have no idea why 8ut that's
-- just how it is, yknow.
--
-- So if you intend to use this to
-- update a bullet position, subtract
-- math.rad(45) from whatever angle
-- you wanted to use.
function rotate(x, y, angle)
local cs = math.cos(angle)
local sn = math.sin(angle)
return {
x = x * cs - y * sn,
y = x * sn + y * cs
}
end
-- This gets the angle from point A to
-- point B.
-- I didn't do super gr8 in like.
-- School. So if you asked me what it
-- actually did I couldn't tell you 8ut
-- I can tell you it works which is good
-- enough for me.
function angle(ax, ay, bx, by)
return math.atan2(by-ay, bx-ax)
end
-- Creates a new entity from scratch.
-- Called every time a shoot is done.
function entity(x, y, angle)
return {
x = x,
y = y,
angle = angle,
speed = 150,
update = function(self, dt)
-- Rotate the speed we're
-- moving this bullet at
-- by the angle from start
-- to where the mouse was at
-- when we shot.
local offset = rotate(
self.speed * dt,
self.speed * dt,
-- Remember velocities are always
-- off by like. 45 degrees.
self.angle - math.rad(45)
)
self.x = self.x + offset.x
self.y = self.y + offset.y
end
}
end
--[[ Calculate out the delta time. ]]
local t = 0
local lt = 0
local dt = 0
-- This just sets where the bullets come
-- from. Move it if you want.
local start = {
x = 120,
y = 60
}
function TIC()
-- Get delta time and reduce it to microseconds
lt = t
t = time()
dt = (t - lt) / 1000
-- Get mouse details.
mouseX, mouseY, down = mouse()
cls()
print(mouseX .. ', ' .. mouseY)
spr(1, start.x + 4, start.y + 4)
if down then
-- Adds a new bullet.
table.insert(entities, entity(start.x, start.y, angle(start.x, start.y, mouseX, mouseY)))
end
for index, entity in pairs(entities) do
entity:update(dt)
spr(2, entity.x + 4, entity.y + 4, 0)
end
end
-- <TILES>
-- 001:0000000000ffff000f0000f00f0ff0f00f0ff0f00f0000f000ffff000f0000f0
-- 002:0000000000000000000ff00000ffff0000ffff00000ff0000000000000000000
-- </TILES>
-- <WAVES>
-- 000:00000000ffffffff00000000ffffffff
-- 001:0123456789abcdeffedcba9876543210
-- 002:0123456789abcdef0123456789abcdef
-- </WAVES>
-- <SFX>
-- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
-- </SFX>
-- <PALETTE>
-- 000:140c1c44243430346d4e4a4e854c30346524d04648757161597dced27d2c8595a16daa2cd2aa996dc2cadad45edeeed6
-- </PALETTE>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment