From c49fa0f7b489c7f062b8b343e313c46fb609f317 Mon Sep 17 00:00:00 2001 From: Madiwka Date: Fri, 21 Jan 2022 15:02:58 +0600 Subject: [PATCH 1/2] Changed zoom, added basic effects, made ship collision a thing and fixed planet clipping --- data/settings.lua | 2 +- import.lua | 3 ++ main.lua | 17 +++++++-- src/calc.lua | 15 ++++++++ src/class/FX.lua | 31 ++++++++++++++++ src/class/Player.lua | 86 ++++++++++++++++++++++++++++++-------------- 6 files changed, 124 insertions(+), 30 deletions(-) create mode 100755 src/class/FX.lua diff --git a/data/settings.lua b/data/settings.lua index f1783b5..67243dc 100755 --- a/data/settings.lua +++ b/data/settings.lua @@ -2,7 +2,7 @@ settings = { zoom = { min = 0.0001, max = 4, - step = 0.01, + step = 0.05, reset = 0.0001 }, warp = { diff --git a/import.lua b/import.lua index 32b0f5a..a8deaea 100755 --- a/import.lua +++ b/import.lua @@ -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" diff --git a/main.lua b/main.lua index d29fa18..a2754fd 100755 --- a/main.lua +++ b/main.lua @@ -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 diff --git a/src/calc.lua b/src/calc.lua index ca4cc81..8c67cb5 100755 --- a/src/calc.lua +++ b/src/calc.lua @@ -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 \ No newline at end of file diff --git a/src/class/FX.lua b/src/class/FX.lua new file mode 100755 index 0000000..7368aaf --- /dev/null +++ b/src/class/FX.lua @@ -0,0 +1,31 @@ +-- Effect object +FX = Class {} + +function FX:init(type, x, y) + 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. +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 > 1000 then + self.finished = true + end +end + + + +function FX:draw() + debug("drawing flash") + if self.type == "flash" then + self:flash() + end +end \ No newline at end of file diff --git a/src/class/Player.lua b/src/class/Player.lua index 712e31c..282d50b 100755 --- a/src/class/Player.lua +++ b/src/class/Player.lua @@ -70,36 +70,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 +151,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 then + -- Add explosion effect? + table.insert(effects, FX("flash", self.x, self.y)) + self:reset() + self.landingspeed = 0 + 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() From 4dbfa5cb5a0d2e4e1aa914b19f836416aaefc315 Mon Sep 17 00:00:00 2001 From: Madiwka Date: Fri, 21 Jan 2022 15:26:49 +0600 Subject: [PATCH 2/2] Fixed explosion detection --- src/class/FX.lua | 7 ++++++- src/class/Player.lua | 35 +++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/class/FX.lua b/src/class/FX.lua index 7368aaf..73fe3f7 100755 --- a/src/class/FX.lua +++ b/src/class/FX.lua @@ -1,13 +1,14 @@ -- Effect object FX = Class {} -function FX:init(type, x, y) +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 @@ -16,6 +17,10 @@ function FX:flash() 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 diff --git a/src/class/Player.lua b/src/class/Player.lua index 282d50b..4bb2520 100755 --- a/src/class/Player.lua +++ b/src/class/Player.lua @@ -26,6 +26,9 @@ function Player:init(tempX, tempY) -- Rotation: self.angle = calc.pi/2 + + -- Status: + self.exploding = false end @@ -153,11 +156,11 @@ end function Player:hasCrashed() --Testing function, see if a player is inside a planet --debug(self.landingspeed) - if self.landingspeed > self.impacttolerance then + if self.landingspeed > self.impacttolerance and not self.exploding then -- Add explosion effect? - table.insert(effects, FX("flash", self.x, self.y)) - self:reset() - self.landingspeed = 0 + table.insert(effects, FX("flash", self.x, self.y, self)) + self.exploding = true + return end end @@ -196,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() @@ -221,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 \ No newline at end of file