Browse Source

Merge pull request #3 from NiroUwU/testing

Collision and crashes
tags/v0.0.6_dev
Madiwka 2 years ago
committed by GitHub
parent
commit
60522207d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 146 additions and 40 deletions
  1. +1
    -1
      data/settings.lua
  2. +3
    -0
      import.lua
  3. +14
    -3
      main.lua
  4. +15
    -0
      src/calc.lua
  5. +36
    -0
      src/class/FX.lua
  6. +77
    -36
      src/class/Player.lua

+ 1
- 1
data/settings.lua View File

@@ -2,7 +2,7 @@ settings = {
zoom = {
min = 0.0001,
max = 4,
step = 0.01,
step = 0.05,
reset = 0.0001
},
warp = {


+ 3
- 0
import.lua View File

@@ -13,10 +13,13 @@ texture = require "textures/textures"
calc = require "src/calc"
font = require "src/font"



-- Game Classes:
require "src/class/Player"
require "src/class/Gui"
require "src/class/Planet"
require "src/class/FX"

-- Game Data:
planetdata = require "data/planetdata"

+ 14
- 3
main.lua View File

@@ -25,6 +25,7 @@ function love.load()
local spawnPlanet = planet[1]
player = Player(spawnPlanet.x, spawnPlanet.y-spawnPlanet.r-1)
gui = Gui(1)
effects = {}
end


@@ -62,7 +63,16 @@ function drawPlanets()
end
end


function drawEffects()
for i=1, #effects do
effects[i]:draw()
end
for i, effect in ipairs(effects) do --Separate functions because if I remove something while processing it it WILL lead to an error
if effect.finished then
table.remove(effects, i)
end
end
end


-- Camera
@@ -75,10 +85,10 @@ function cameraControls()
function love.wheelmoved(x, y)
if y > 0 then
-- Zoom in:
zoomlevel = zoomlevel + step
zoomlevel = zoomlevel + step*(zoomlevel*10)
elseif y < 0 then
-- Zoom out:
zoomlevel = zoomlevel - step
zoomlevel = zoomlevel - step*(zoomlevel*10)
end
end

@@ -162,6 +172,7 @@ function love.draw()
cam:attach()
-- Game Objects:
drawPlanets()
drawEffects()
player:draw()

-- Camera Zoom Player Location Indicator: OVERWORK SOON PLS KAY; IT UGLY


+ 15
- 0
src/calc.lua View File

@@ -36,5 +36,20 @@ function calc.gPull(obj1, obj2)
return grav
end

-- Calculate closest space object to target:
function calc.closestObj(target)
local minDist = calc.distance(planet[1].x, planet[1].y, target.x, target.y)-planet[1].r
local minPlanet = planet[1]
for i, pla in ipairs(planet) do
if calc.distance(pla.x, pla.y, target.x, target.y)-pla.r < minDist then
minDist = calc.distance(pla.x, pla.y, target.x, target.y)-pla.r
minPlanet = pla
end
end

return minPlanet

end


return calc

+ 36
- 0
src/class/FX.lua View File

@@ -0,0 +1,36 @@
-- Effect object
FX = Class {}

function FX:init(type, x, y, target)
self.type = type
self.id = #effects
self.frame = 0
self.x = x
self.y = y
self.finished = false -- So that main can kill it later. For some reason it does not want to kill itself.
self.target = target
end


function FX:flash()
love.graphics.setColor(1,1,1,1-self.frame/1000)
love.graphics.circle("fill", self.x, self.y, self.frame)
self.frame = self.frame + 100/love.timer.getFPS()
--debug("Frame is " .. self.frame)
if self.frame > 500 then
self.target.exploding = false
self.target:reset()
end
if self.frame > 1000 then
self.finished = true
end
end



function FX:draw()
debug("drawing flash")
if self.type == "flash" then
self:flash()
end
end

+ 77
- 36
src/class/Player.lua View File

@@ -26,6 +26,9 @@ function Player:init(tempX, tempY)

-- Rotation:
self.angle = calc.pi/2

-- Status:
self.exploding = false
end


@@ -70,36 +73,60 @@ function Player:reset()
end

function Player:flightControls()
-- Anti-clipping feature
local closestPla = calc.closestObj(player)


-- Movement:
local speedChange = self.speed * self.throttle

-- Directional Thrust:
if love.keyboard.isDown(controls.flight.thrust.up)then
self.ySpeed = self.ySpeed - speedChange
debug("Player control: up")
if (calc.distance(closestPla.x, closestPla.y, self.x, self.y) > calc.distance(closestPla.x, closestPla.y, self.x, self.y-10) and self:isLanded()) then
debug("Flying into a planet!")
else
self.ySpeed = self.ySpeed - speedChange
debug("Player control: up")
end
end

if love.keyboard.isDown(controls.flight.thrust.down) then
self.ySpeed = self.ySpeed + speedChange
debug("Player control: down")
if (calc.distance(closestPla.x, closestPla.y, self.x, self.y) > calc.distance(closestPla.x, closestPla.y, self.x, self.y+10) and self:isLanded()) then
debug("Flying into a planet!")
else
self.ySpeed = self.ySpeed + speedChange
debug("Player control: down")
end
end

if love.keyboard.isDown(controls.flight.thrust.left) then
self.xSpeed = self.xSpeed - speedChange
debug("Player control: left")
if (calc.distance(closestPla.x, closestPla.y, self.x, self.y) > calc.distance(closestPla.x, closestPla.y, self.x-10, self.y) and self:isLanded()) then
debug("Flying into a planet!")
else
self.xSpeed = self.xSpeed - speedChange
debug("Player control: left")
end
end

if love.keyboard.isDown(controls.flight.thrust.right) then
self.xSpeed = self.xSpeed + speedChange
debug("Player control: right")
if (calc.distance(closestPla.x, closestPla.y, self.x, self.y) > calc.distance(closestPla.x, closestPla.y, self.x+10, self.y) and self:isLanded()) then
debug("Flying into a planet!")
else
self.xSpeed = self.xSpeed + speedChange
debug("Player control: right")
end
end


-- Main Engine controls:
if love.keyboard.isDown(controls.flight.thrust.engine) then
self.xSpeed = self.xSpeed + math.cos(self.angle) * speedChange
debug("Ship thrusters X: " .. math.cos(self.angle) .. " " .. math.sin(self.angle) .. " " .. self.angle)
self.ySpeed = self.ySpeed - math.sin(self.angle) * speedChange
if (calc.distance(closestPla.x, closestPla.y, self.x, self.y) > calc.distance(closestPla.x, closestPla.y, self.x+self.xSpeed + math.cos(self.angle) * speedChange*2, self.y+self.ySpeed - math.sin(self.angle) * speedChange*2) and self:isLanded()) then --Holy moly this is long
debug("Flying into a planet!")
else
self.xSpeed = self.xSpeed + math.cos(self.angle) * speedChange*2
debug("Ship thrusters X: " .. math.cos(self.angle) .. " " .. math.sin(self.angle) .. " " .. self.angle)
self.ySpeed = self.ySpeed - math.sin(self.angle) * speedChange*2
end
end
if love.keyboard.isDown(controls.flight.thrust.rotleft) then
self.angle = self.angle + 1/love.timer.getFPS()
@@ -127,21 +154,31 @@ function Player:getSpeed()
return x+y
end

function Player:isLanded()
local landed = false
for i=1, #planet do
local pla = planet[i]
if calc.distance(self.x, self.y, pla.x, pla.y) <= pla.r then
landed = true
end
end
-- Save Landing Speed:
if landed then
self.landingspeed = self:getSpeed()
--debug("Landing speed: "..self.landingspeed)
end
function Player:hasCrashed() --Testing function, see if a player is inside a planet
--debug(self.landingspeed)
if self.landingspeed > self.impacttolerance and not self.exploding then
-- Add explosion effect?
table.insert(effects, FX("flash", self.x, self.y, self))
self.exploding = true
return
end
end

return landed
function Player:isLanded()
local landed = false
for i=1, #planet do
local pla = planet[i]
if calc.distance(self.x, self.y, pla.x, pla.y) <= pla.r then
landed = true
end
end
-- Save Landing Speed:
if landed then
self.landingspeed = self:getSpeed()
debug("Landing speed: "..self.landingspeed)
end
self:hasCrashed()
return landed
end

function Player:gravity()
@@ -162,15 +199,17 @@ end

function Player:update(dt)
--debug(self.warpspeed)
if self.angle > calc.pi*2 then
self.angle = 0
elseif self.angle < 0 then
self.angle = calc.pi*2
end
self:gravity()
self:flightControls()
self:updatePosition()
if not self.exploding then
self:gravity()
self:flightControls()
self:updatePosition()
end
end

function Player:draw()
@@ -187,12 +226,14 @@ function Player:draw()
-- Right Bottom
x+dist, y+dist
}
love.graphics.setColor(0.5, 0.5, 0.7)
love.graphics.polygon("fill", vertices)
if not self.exploding then
love.graphics.setColor(0.5, 0.5, 0.7)
love.graphics.polygon("fill", vertices)

love.graphics.setColor(1, 0, 0)
love.graphics.circle("fill", x, y, 5, 20)
love.graphics.setColor(1, 0, 0)
love.graphics.circle("fill", x, y, 5, 20)

-- Directional Circle (temporary until actual rotatable ship texture is made)
love.graphics.circle("fill", x+dist*(1/zoomlevel*2)*math.cos(self.angle), y-dist*(1/zoomlevel*2)*math.sin(self.angle), 1/zoomlevel*2)
-- Directional Circle (temporary until actual rotatable ship texture is made)
love.graphics.circle("fill", x+dist*(1/zoomlevel*2)*math.cos(self.angle), y-dist*(1/zoomlevel*2)*math.sin(self.angle), 1/zoomlevel*2)
end
end

Loading…
Cancel
Save