fysplane/entities/plane.lua

415 lines
16 KiB
Lua
Raw Normal View History

Class = require 'hump.class'
require 'entities/physicsentity'
2014-10-18 14:56:40 +00:00
require 'settings'
require 'entities/debug'
require 'entities/animation'
2014-10-20 19:55:32 +00:00
require 'entities/ground'
2014-10-20 20:32:25 +00:00
require 'entities/arrow'
2014-10-18 16:33:32 +00:00
Matrix = require 'matrix'
2014-10-18 14:56:40 +00:00
VectorLight = require 'hump/vector-light'
require 'utils'
require 'machinegun'
2014-10-19 02:15:37 +00:00
local CLANG_SFX = {
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #1.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #2.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #3.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #4.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #5.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #6.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #7.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #8.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #9.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #10.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #11.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #12.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #13.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #14.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #15.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #16.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #17.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #18.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #19.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #20.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #21.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #22.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #23.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #24.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #25.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #26.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #27.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #28.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #29.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #30.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #31.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #32.wav"),
love.audio.newSource("resources/audio/clangs/cast iron clangs - Marker #33.wav")
}
2014-10-18 16:33:32 +00:00
local function sign(v)
if v >= 0 then
return 1
else
return -1
end
end
2014-10-19 01:49:13 +00:00
local lift = function(angle)
return 1.68429 * math.exp(-math.pow(angle / math.pi * 180.0 -17.3801, 2.0) / (2.0 * math.pow(15.0, 2.0)))
end
2014-10-20 17:50:31 +00:00
local fwd_frict_coeff = 0.1
local fwd_frict_coeff_decelare_multiplier = 3
2014-10-20 17:50:31 +00:00
local nor_frict_coeff = 0.8
local tail_frict_coeff = 0.4
2014-10-19 10:13:47 +00:00
local tail_area = 5.0
2014-10-20 17:35:39 +00:00
local turn_coeff = 500.0
2014-10-20 20:42:57 +00:00
local wing_lift = 0.3
2014-10-19 01:49:13 +00:00
local accel_speed = 400.0
local decel_speed = 400.0
2014-10-18 23:22:14 +00:00
local max_motorPower = ENGINE_MAX
local head_area = 1.0
2014-10-19 10:13:47 +00:00
local plane_area = 10.0
2014-10-20 17:50:31 +00:00
local motor_speed_ratio = 80.0
2014-10-18 16:33:32 +00:00
local out_of_bounds_coeff_multiplier = 10.0
local explosionFrames = AnimationFrames("resources/graphics/explosion-%04d.png", 36, 15, true)
2014-10-19 00:47:08 +00:00
Plane = Class{
__includes = PhysicsEntity,
2014-10-18 21:24:01 +00:00
frames = {},
2014-10-19 10:13:47 +00:00
motorPower = INITIAL_ENGINE_SPEED,
2014-10-18 14:56:40 +00:00
debugVectors = {},
turningCw = false,
turningCcw = false,
accelerating = false,
decelerating = false,
2014-10-18 22:15:31 +00:00
goingRight = true, -- the plane is upside up and going right (or upside down and going left)
orientationAngle = 0,
2014-10-20 18:08:28 +00:00
init = function(self, x, y, xDir, yDir, r, g, b, level)
2014-10-18 22:53:08 +00:00
local density = 50
PhysicsEntity.init(self, x, y, level, "dynamic", 0.2)
2014-10-20 18:08:28 +00:00
self.r = r
self.g = g
self.b = b
self.level = level
2014-10-18 22:01:37 +00:00
self.xsize = 4.0 * PIXELS_PER_METER
self.ysize = 4.3 * PIXELS_PER_METER
self.shape = love.physics.newRectangleShape(self.xsize, self.ysize)
PhysicsEntity.attachShape(self, density)
self.body:setX(self.x + self.xsize / 2)
self.body:setY(self.y - self.ysize / 2)
2014-10-18 22:15:31 +00:00
self.body:setLinearVelocity(xDir, yDir)
self.goingRight = xDir >= 0
self.body:setAngle(math.atan2(yDir, xDir))
2014-10-18 22:01:37 +00:00
-- self.body:setMassData(self.xsize / 2, self.ysize / 2, 440 * PIXELS_PER_METER, -1.0)
-- self.body:setMassData(self.xsize / 2, 0, 430, 158194)
2014-10-19 10:13:47 +00:00
self.body:setMassData(7, 5, 800, 558194.140625)
self.body:setAngularDamping(0.1)
2014-10-20 19:55:32 +00:00
self.fixture:setFriction(0.5)
self.angle = 0
2014-10-18 22:01:37 +00:00
--x 16 y 17.200000762939 mass 3520 inertia 1942476.875
--x 0 y 0 mass 860.00006103516 inertia 158194.140625
2014-10-19 10:13:47 +00:00
local x, y, mass, inertia = self.body:getMassData()
2014-10-18 22:01:37 +00:00
print("x", x, "y", y, "mass", mass, "inertia", inertia)
2014-10-18 21:24:01 +00:00
for frame = 0,35 do
self.frames[frame] = love.graphics.newImage(string.format("resources/graphics/plane-%04d.png", frame))
end
self.quad = love.graphics.newQuad(0, 0, self.xsize, self.ysize, self.frames[0]:getWidth(), self.frames[0]:getHeight())
2014-10-18 22:46:27 +00:00
self.machinegun = MachineGun(self, PLANE_DEFAULT_GUN)
2014-10-18 20:27:40 +00:00
self.health = PLANE_HEALTH
2014-10-18 22:46:27 +00:00
self.powerupmode = nil
2014-10-20 20:32:25 +00:00
self.outOfBoundsArrow = nil
end;
2014-10-19 01:35:07 +00:00
receiveDamage = function(self, amount)
local oldHealth = self.health
2014-10-18 22:51:57 +00:00
self.health = math.max(0, self.health - amount);
if oldHealth > 0 and self.health == 0 then
2014-10-19 01:35:07 +00:00
self:die()
end
2014-10-18 22:51:57 +00:00
end;
2014-10-19 01:35:07 +00:00
die = function(self)
2014-10-19 00:41:25 +00:00
self.health = 0
2014-10-19 10:13:47 +00:00
self.machinegun:stopShooting()
2014-10-19 00:47:08 +00:00
Animation(self.body:getX(), self.body:getY(), self.level, explosionFrames)
2014-10-19 00:41:25 +00:00
self:getOwner():setPlane(nil)
self:delete()
end;
2014-10-20 20:32:25 +00:00
delete = function(self)
PhysicsEntity.delete(self)
end;
getGunPosition = function(self)
local x = self.body:getX()
local y = self.body:getY()
2014-10-19 02:15:37 +00:00
local pos = rad_dist_to_xy(self.angle, self.xsize)
return {x + pos[1], y + pos[2]}
end;
2014-10-18 22:46:27 +00:00
setPowerUpMode = function(self, powerupmode)
2014-10-18 23:02:50 +00:00
print("Got powerup!")
2014-10-18 22:46:27 +00:00
self.powerupmode = powerupmode
self.powerupmode:activate(self)
end;
accelerate = function(self, down)
self.accelerating = down
end;
decelerate = function(self, down)
self.decelerating = down
end;
shoot = function(self, down)
2014-10-19 10:13:47 +00:00
if self.health > 0 then
if down then
self.machinegun:startShooting()
else
self.machinegun:stopShooting()
end
end
end;
wasHitBy = function(self, by)
2014-10-20 19:55:32 +00:00
if by:isinstance(Ground) then
local angle = self.body:getAngle()
if (not goingRight and (angle > -math.pi / 4 and angle < math.pi / 4)) or
(goingRight and (angle > math.pi - math.pi / 4 and angle < math.pi + math.pi / 4)) then
-- don't die this time.
else
self:receiveDamage(1000)
self:getOwner():addScore(SUICIDE_SCORE)
end
elseif not by:isinstance(Ground) then
local i = love.math.random(#CLANG_SFX)
CLANG_SFX[i]:play()
end
2014-10-19 02:15:37 +00:00
end;
update = function(self, dt)
2014-10-19 10:13:47 +00:00
local mass_x, mass_y, mass, inertia = self.body:getMassData()
self.debug = {}
PhysicsEntity.update(self, dt)
self.machinegun:update(dt)
local coeff_multiplier = 1
if self.body:getY() < 0 then
self.motorPower = 0
coeff_multiplier = out_of_bounds_coeff_multiplier
2014-10-20 20:32:25 +00:00
if not self.outOfBoundsArrow then
self.outOfBoundsArrow = Arrow(0, 0, self.level, self.r, self.g, self.b)
end
self.outOfBoundsArrow:setX(self.body:getX())
elseif self.outOfBoundsArrow then
self.outOfBoundsArrow:delete()
self.outOfBoundsArrow = nil
end
2014-10-18 22:46:27 +00:00
if self.powerupmode ~= nil then
self.powerupmode:update(dt)
end
self.x, self.y = self.fixture:getBoundingBox()
self.angle = self.body:getAngle()
2014-10-18 14:56:40 +00:00
2014-10-18 16:33:32 +00:00
local vel_x, vel_y = self.body:getLinearVelocity()
local abs_vel = VectorLight.len(vel_x, vel_y)
if self.accelerating then
self.motorPower = math.min(max_motorPower, self.motorPower + dt * accel_speed)
end
if self.decelerating then
self.motorPower = math.max(0.0, self.motorPower - dt * accel_speed)
end
2014-10-18 16:33:32 +00:00
-- -- local base =
-- let base = V.base (V.vec_of_ang (~-(body#get_angle))) in
local basde = Matrix{{math.cos(-self.angle),
math.sin(-self.angle)},
{math.cos(-self.angle + math.pi / 2),
math.sin(-self.angle + math.pi / 2)}}
2014-10-18 16:33:32 +00:00
local to_base = function(x, y)
local m = Matrix.mul(basde, Matrix{{x}, {y}})
-- print("to_base", x, y, "->", Matrix.tostring(m), " = ", m[1][1], m[2][1])
return m
2014-10-18 16:33:32 +00:00
end
-- print("-")
-- print(Matrix.tostring(base))
2014-10-18 16:33:32 +00:00
local fwd_x, fwd_y = VectorLight.rotate(self.angle, 1, 0)
local normal_x, normal_y = VectorLight.perpendicular(fwd_x, fwd_y)
2014-10-18 16:33:32 +00:00
local fwd_vel = VectorLight.dot(vel_x, vel_y, fwd_x, fwd_y)
local normal_vel = VectorLight.dot(vel_x, vel_y, normal_x, normal_y)
-- print("fwd_vel", fwd_vel, "normal_vel", normal_vel, "vel_x", vel_x, "vel_y", vel_y, "normal_x", normal_x, "normal_y", normal_y)
2014-10-19 10:13:47 +00:00
table.insert(self.debug, DebugVector("vel", self.body:getX(), self.body:getY(), vel_x, vel_y))
table.insert(self.debug, DebugVector("fwd", self.body:getX(), self.body:getY(), fwd_x, fwd_y))
table.insert(self.debug, DebugVector("normal", self.body:getX(), self.body:getY(), normal_x, normal_y))
2014-10-18 16:33:32 +00:00
2014-10-19 10:13:47 +00:00
local tail_speed = self.body:getAngularVelocity() * math.pi * 2.0 * self.xsize / 2.0 / PIXELS_PER_METER
-- print("angular velocity", self.body:getAngularVelocity())
2014-10-18 16:33:32 +00:00
local tail_vel = to_base(0, tail_speed) -- hmm?! not tail_speed, 0?
local abs_tail_vel_x, abs_tail_vel_y = VectorLight.add(vel_x, vel_y, tail_vel[1][1], tail_vel[2][1])
2014-10-19 10:13:47 +00:00
-- table.insert(self.debug, DebugVector("tail", self.body:getX(), self.body:getY(), tail_vel[1][1], tail_vel[2][1]))
2014-10-18 16:33:32 +00:00
--print("absolute tail veloicty: ", abs_tail_vel_x, abs_tail_vel_y)
local head_angle = self.angle
local rel_force = function(label, force_x, force_y, rel_at_x, rel_at_y)
2014-10-19 10:13:47 +00:00
if label == "turn" or
label == "lift" or
label == "hddrag" or
label == "airdrag" or
label == "tf" or
label == "motor" then
-- rdx, rdy = VectorLight.add(rdx, rdy, self.body:getX(), self.body:getY())
local base_force = to_base(force_x, force_y)
local base_rel_at = to_base(rel_at_x, rel_at_y)
base_rel_at_x, base_rel_at_y = VectorLight.add(base_rel_at[1][1], base_rel_at[2][1], self.body:getX(), self.body:getY())
self.body:applyForce(base_force[1][1], base_force[2][1], base_rel_at_x, base_rel_at_y)
at_x, at_y = VectorLight.add(base_rel_at_x, base_rel_at_y, base_force[1][1], base_force[2][1])
--print(at_x, at_y)
-- print("base matrix", Matrix.tostring(base))
-- print("force", force_x, force_y)
-- print("force after transformation", Matrix.tostring(to_base(force_x, force_y)))
-- print("base_force", base_force[1][1], base_force[2][1])
-- print("base_force'", Matrix.tostring(base_force))
table.insert(self.debug, DebugVector(label, base_rel_at_x, base_rel_at_y, base_force[1][1], base_force[2][1]))
end
2014-10-18 16:33:32 +00:00
end
local speed_angle
if abs_vel < 1.0 then
speed_angle = head_angle
else
speed_angle = math.atan2(vel_y, vel_x)
end
local air_wing_angle
local tmp
tmp = head_angle - speed_angle
if tmp > math.pi then
air_wing_angle = tmp - 2.0 * math.pi
else
air_wing_angle = tmp
end
2014-10-19 10:13:47 +00:00
local lift_coeff = lift(-air_wing_angle)
2014-10-18 16:33:32 +00:00
2014-10-18 16:59:37 +00:00
-- print("lift coeff: ", lift_coeff)
2014-10-18 16:33:32 +00:00
-- motor
2014-10-19 10:13:47 +00:00
rel_force("motor",
self.motorPower * motor_speed_ratio * PIXELS_PER_METER, 0,
mass_x + 0.2, mass_y + 0.4)
--self.body:applyForce(dx, dy);
-- table.insert(self.debug, DebugVector(self.body:getX(), self.body:getY(), dx, dy))
2014-10-18 14:56:40 +00:00
-- (* self#add_force "fwddrag" *)
-- (* (Gg.V2.smul (fwd_frict_coeff *. fwd_vel ** 2.0 *. head_area) (V.unit (negate vel))) *)
-- (* (to_base (Gg.V2.v 0.0 0.0)); *)
local airdrag = coeff_multiplier * -fwd_frict_coeff * math.pow(fwd_vel, 2.0) * head_area
if self.decelerating then
airdrag = airdrag * fwd_frict_coeff_decelare_multiplier
end
2014-10-18 22:01:37 +00:00
rel_force("airdrag", airdrag, 0, 0, 0.2 * self.ysize)
2014-10-18 16:33:32 +00:00
-- Air friction (and drag?) opposes movement towards plane velocity normal also
-- hdd drag
local hddrag_x, hddrag_y = VectorLight.mul(coeff_multiplier * nor_frict_coeff * math.pow(normal_vel, 2.0) * plane_area * sign(normal_vel), 0, -1.0)
2014-10-19 10:13:47 +00:00
rel_force("hddrag", hddrag_x, hddrag_y, mass_x, mass_y);
2014-10-18 16:33:32 +00:00
local lift_x, lift_y = VectorLight.mul(wing_lift * math.pow(fwd_vel, 2.0) * lift_coeff, 0, -1)
2014-10-19 10:13:47 +00:00
rel_force("lift", lift_x, lift_y, mass_x, mass_y)
2014-10-18 16:33:32 +00:00
local tail_frict = tail_area * coeff_multiplier * tail_frict_coeff * math.pow(normal_vel, 2.0) * -sign(normal_vel)
2014-10-19 10:13:47 +00:00
-- print("tail_speed", tail_speed)
2014-10-18 23:45:49 +00:00
rel_force("tf", 0, tail_frict, -self.xsize / 2, 0)
-- if self.turningCcw then
-- dx, dy = VectorLight.rotate(self.angle, 0, 50000)
-- elseif self.turningCw then
-- dx, dy = VectorLight.rotate(self.angle, 0, -50000)
-- end
2014-10-18 14:56:40 +00:00
if self.turningCcw then
2014-10-20 17:35:39 +00:00
dx, dy = 0, 1
2014-10-19 10:13:47 +00:00
elseif self.turningCw then
2014-10-20 17:35:39 +00:00
dx, dy = 0, -1
end
if self.turningCw or self.turningCcw then
2014-10-20 17:35:39 +00:00
dx, dy = VectorLight.mul(fwd_vel * turn_coeff, dx, dy)
-- dx, dy = VectorLight.mul(turn_coeff * 10000 -- * sign(fwd_vel)
2014-10-19 10:13:47 +00:00
-- , dx, dy)
-- print("dx", dx, "dy", dy, "tail_speed", tail_speed)
-- self.body:applyForce(dx, dy, self.body:getX() + rdx, self.body:getY() + rdy)
rel_force("turn", dx, dy, -0.4 * self.xsize, 0);
2014-10-18 14:56:40 +00:00
end
-- print(dx, dy)
2014-10-18 14:56:40 +00:00
2014-10-19 10:13:47 +00:00
-- table.insert(self.debug, DebugVector(self.x + width / 2, self.y + height / 2, 0, -100))
-- table.insert(self.debug, DebugVector(self.x, self.y, -100, 0))
-- table.insert(self.debug, DebugVector(self.x + 55, self.y + 10, dx, dy)
local xy = to_base(mass_x, mass_y)
local x, y = VectorLight.add(xy[1][1], xy[2][1], self.body:getX(), self.body:getY())
table.insert(self.debug, DebugCircle("mc", x, y))
end;
draw = function(self)
2014-10-18 22:51:57 +00:00
if self.health > 0 then
PhysicsEntity.draw(self)
love.graphics.push()
2014-10-20 18:08:28 +00:00
love.graphics.setColor(self.r, self.g, self.b);
2014-10-18 22:51:57 +00:00
if self.goingRight then
love.graphics.translate(self.body:getX(), self.body:getY())
love.graphics.scale(-1, 1)
love.graphics.draw(self.frames[math.floor(self.orientationAngle / 10.0)], self.quad, 0, 0, -self.angle, 1, 1, self.xsize / 2, self.ysize / 2)
2014-10-18 22:51:57 +00:00
else
love.graphics.translate(self.body:getX(), self.body:getY())
2014-10-18 23:25:41 +00:00
love.graphics.scale(-1, -1)
love.graphics.draw(self.frames[math.floor(self.orientationAngle / 10.0)], self.quad, 0, 0, self.angle, 1, 1, self.xsize / 2, self.ysize / 2)
2014-10-18 22:51:57 +00:00
end
love.graphics.pop()
2014-10-19 03:51:53 +00:00
2014-10-19 10:13:47 +00:00
drawDebug(self.debug)
2014-10-18 22:15:31 +00:00
end
end;
cw = function(self, isTurning)
self.turningCw = isTurning
end;
ccw = function(self, isTurning)
self.turningCcw = isTurning
end;
}