Ver a proveniência

Added hierarchical gravity, ability to make child objects in space

tags/v0.0.6_dev
Madi há 2 anos
ascendente
cometimento
7c51c7032f
4 ficheiros alterados com 76 adições e 21 eliminações
  1. +9
    -1
      data/planetdata.lua
  2. +10
    -5
      main.lua
  3. +56
    -3
      src/class/Planet.lua
  4. +1
    -12
      src/class/Player.lua

+ 9
- 1
data/planetdata.lua Ver ficheiro

@@ -3,7 +3,15 @@ planetdata = {
x = 0, y = 50000,
r = 12000, m = 5e14,
name = "Testos",
colour = {42, 04, 20}
colour = {42, 04, 20},
parent = "star"
},
{
x = 30000, y = 50000,
r = 2000, m = 1e14,
name = "Deez",
colour = {50, 50, 50},
parent = 1
}
}


+ 10
- 5
main.lua Ver ficheiro

@@ -8,9 +8,13 @@ function love.load()
love.window.setTitle(info.name.." - v"..info.version)
width, height = love.graphics.getDimensions()
cam = Camera()
zoomlevel = 1
zoomlevel = 0.005



-- Loading:
ships = {} --Potentially add other starships in the future?

planet = {}
loadPlanets()

@@ -33,7 +37,8 @@ function loadPlanets()
p.x, p.y,
p.r, p.m,
p.name,
p.colour
p.colour,
p.parent
)
)
debug(p.name.." is loaded")
@@ -42,14 +47,13 @@ function loadPlanets()
end

function updatePlanets()
for i=1, #planet do
planet[i]:update()
end
planet[1]:update()
end

function drawPlanets()
for i=1, #planet do
planet[i]:draw()
--debug("Drawing planet " .. i)
end
end

@@ -106,6 +110,7 @@ function love.update(dt)
-- Camera:
cam:lookAt(player.x, player.y)
cameraControls()
--debug(player.x .. " " .. player.y)
end

function love.draw()


+ 56
- 3
src/class/Planet.lua Ver ficheiro

@@ -1,33 +1,86 @@
Planet = Class {}

function Planet:init(tempX, tempY, tempR, tempM, tempName, tempC)
-- Planet Position
function Planet:init(tempX, tempY, tempR, tempM, tempName, tempC, tempP)
-- Planet Position:
self.x = tempX
self.y = tempY

-- Planet Radius and Mass
-- Speed:
self.xSpeed = 0
self.ySpeed = 0

-- Speed Change: (throttle 0 - 1; variable) (speed; constant (max speed change))
self.throttle = 0.5
self.speed = 0.05

-- Planet Radius and Mass:
self.r = tempR
self.m = tempM

-- Planet Data:
self.name = tempName
self.colour = tempC

-- Planet Family:
self.children = {}
if (tempP ~= "star") then
self.ySpeed = 5
self.parent = planet[tempP]
table.insert(planet[tempP].children, self)
end


end




-- FUNCTIONS

function Planet:updatePosition()
self.x = self.x + self.xSpeed
self.y = self.y + self.ySpeed
debug("Updating position of planet " .. self.name .. ": " .. self.x .. " " .. self.y)
end

function Planet:attract(dt) --Planet doing the attracting, divided in two parts:
--Attracting children
for i, child in ipairs(self.children) do
local grav = calc.gPull(self, child)
local dist = calc.distance(self.x, self.y, child.x, child.y)
local pull = 20/dist * grav/1e14
child.xSpeed = child.xSpeed - (child.x - self.x)*pull
child.ySpeed = child.ySpeed - (child.y - self.y)*pull
end

--Attracting the player


-- IF IN SPHERE OF INFLUENCE
local grav = calc.gPull(self, player)
local dist = calc.distance(self.x, self.y, player.x, player.y)
local pull = 20/dist * grav

player.xSpeed = player.xSpeed - (player.x - self.x)*pull
player.ySpeed = player.ySpeed - (player.y - self.y)*pull
end


-- MAIN

function Planet:update(dt)
self:attract(dt)
self:updatePosition()
for i, child in ipairs(self.children) do
child:update(dt)
end
end


function Planet:draw()
local col = self.colour
love.graphics.setColor(calc.c(col[1]), calc.c(col[2]), calc.c(col[3]))
love.graphics.circle("fill", self.x, self.y, self.r)
end

+ 1
- 12
src/class/Player.lua Ver ficheiro

@@ -29,7 +29,7 @@ function Player:init(tempX, tempY)
self.landingspeed = 0

-- Mass:
self.m = 1
self.m = 10
end


@@ -137,17 +137,6 @@ function Player:gravity()
if self:isLanded() then
-- Player is landed:
self.xSpeed, self.ySpeed = 0, 0
else
-- Player is not landed:
for i=1, #planet do
local pla = planet[i]
local grav = calc.gPull(self, pla)
local dist = calc.distance(self.x, self.y, pla.x, pla.y)
local pull = 20/dist * grav

self.xSpeed = self.xSpeed + (pla.x - self.x)*pull
self.ySpeed = self.ySpeed + (pla.y - self.y)*pull
end
end
end



Carregando…
Cancelar
Guardar