summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2016-03-20 16:17:30 +0100
committerGravatar alecdwm 2016-03-20 16:17:30 +0100
commit500bec44f5df12415a0bb5370f71425df7b50096 (patch)
treea70dec58ab2f445bfb1f036b5ab4fb137a3facbe
parentb0f187e96d4c16fe97e9da2c0fe00572311c0dd3 (diff)
Multiple changes:
* Camera centers on ship, even when following player * Added functions to find camera bounds in world coordinates * Added a basic starfield from -1000,-1000 to 1000,1000 * Added simple ship-image-to-ship-map parser * Updated ship design * Added some simple sfx
-rw-r--r--prototype/camera.lua50
-rw-r--r--prototype/computerbeep.mp3bin0 -> 35361 bytes
-rw-r--r--prototype/computerbeep.oggbin0 -> 57711 bytes
-rw-r--r--prototype/door.oggbin0 -> 25732 bytes
-rw-r--r--prototype/fire.oggbin0 -> 4643 bytes
-rw-r--r--prototype/main.lua17
-rw-r--r--prototype/player.lua51
-rw-r--r--prototype/ship.lua161
-rw-r--r--prototype/ship.pngbin350 -> 705 bytes
-rw-r--r--prototype/stars.lua28
-rw-r--r--prototype/thrust.oggbin0 -> 4736 bytes
11 files changed, 242 insertions, 65 deletions
diff --git a/prototype/camera.lua b/prototype/camera.lua
index b4d4a34..a04c0a2 100644
--- a/prototype/camera.lua
+++ b/prototype/camera.lua
@@ -8,8 +8,8 @@ camera.follow = "player"
function camera:update(dt)
if camera.follow == "player" then
- camera.x = player.worldX
- camera.y = player.worldY
+ camera.x = player.parent.x
+ camera.y = player.parent.y
camera.rot = player.parent.rot
elseif camera.follow == "ship" then
camera.x = ship.x
@@ -59,3 +59,49 @@ end
function camera:mousePosition()
return love.mouse.getX() * self.sx + self.x, love.mouse.getY() * self.sy + self.y
end
+
+function camera:getBoundsWorldCoords()
+ -- get the camera bounds (top left, top right, bottom left, bottom right) in world space
+ norotxtl = self.x - love.graphics.getWidth() / 2 * self.sx
+ norotytl = self.y - love.graphics.getHeight() / 2 * self.sy
+
+ norotxtr = self.x + love.graphics.getWidth() / 2 * self.sx
+ norotytr = self.y - love.graphics.getHeight() / 2 * self.sy
+
+ norotxbl = self.x - love.graphics.getWidth() / 2 * self.sx
+ norotybl = self.y + love.graphics.getHeight() / 2 * self.sy
+
+ norotxbr = self.x + love.graphics.getWidth() / 2 * self.sx
+ norotybr = self.y + love.graphics.getHeight() / 2 * self.sy
+
+ -- orientate with camera rotation
+ xtl = self.x + (norotxtl-self.x) * math.cos(self.rot) + (norotytl-self.y) * math.sin(self.rot)
+ ytl = self.y + (norotxtl-self.x) * math.sin(self.rot) - (norotytl-self.y) * math.cos(self.rot)
+
+ xtr = self.x + (norotxtr-self.x) * math.cos(self.rot) + (norotytr-self.y) * math.sin(self.rot)
+ ytr = self.y + (norotxtr-self.x) * math.sin(self.rot) - (norotytr-self.y) * math.cos(self.rot)
+
+ xbl = self.x + (norotxbl-self.x) * math.cos(self.rot) + (norotybl-self.y) * math.sin(self.rot)
+ ybl = self.y + (norotxbl-self.x) * math.sin(self.rot) - (norotybl-self.y) * math.cos(self.rot)
+
+ xbr = self.x + (norotxbr-self.x) * math.cos(self.rot) + (norotybr-self.y) * math.sin(self.rot)
+ ybr = self.y + (norotxbr-self.x) * math.sin(self.rot) - (norotybr-self.y) * math.cos(self.rot)
+
+ return xtl, ytl, xtr, ytr, xbl, ybl, xbr, ybr
+end
+
+function camera:inBoundsWorldCoords(x, y)
+ local ax, ay, bx, by, cx, cy, dx, dy = self:getBoundsWorldCoords()
+
+ bax = bx - ax
+ bay = by - ay
+ dax = dx - ax
+ day = dy - ay
+
+ if ((x - ax) * bax + (y - ay) * bay < 0) then return false end
+ if ((x - bx) * bax + (y - by) * bay > 0) then return false end
+ if ((x - ax) * dax + (y - ay) * day < 0) then return false end
+ if ((x - dx) * dax + (y - dy) * day > 0) then return false end
+
+ return true
+end
diff --git a/prototype/computerbeep.mp3 b/prototype/computerbeep.mp3
new file mode 100644
index 0000000..1016126
--- /dev/null
+++ b/prototype/computerbeep.mp3
Binary files differ
diff --git a/prototype/computerbeep.ogg b/prototype/computerbeep.ogg
new file mode 100644
index 0000000..7d127bc
--- /dev/null
+++ b/prototype/computerbeep.ogg
Binary files differ
diff --git a/prototype/door.ogg b/prototype/door.ogg
new file mode 100644
index 0000000..3dd3f03
--- /dev/null
+++ b/prototype/door.ogg
Binary files differ
diff --git a/prototype/fire.ogg b/prototype/fire.ogg
new file mode 100644
index 0000000..69ccfc9
--- /dev/null
+++ b/prototype/fire.ogg
Binary files differ
diff --git a/prototype/main.lua b/prototype/main.lua
index 0d938c2..5adc807 100644
--- a/prototype/main.lua
+++ b/prototype/main.lua
@@ -1,12 +1,14 @@
require "camera"
require "player"
require "ship"
+require "stars"
function love.load()
-- don't fuck with the pixel art, man
love.graphics.setDefaultFilter("nearest", "nearest", 0)
love.graphics.setNewFont("DejaVuSansMono.ttf", 11)
+ stars:load()
player:load()
ship:load()
player:setParent(ship)
@@ -16,10 +18,19 @@ function love.load()
camera:setScale(0.05, 0.05)
end
+function love.update(dt)
+ ship:update(dt)
+ player:update(dt)
+ camera:update(dt)
+ stars:update(dt)
+end
+
function love.draw()
-- World
camera:set()
+ stars:draw()
+
love.graphics.setColor(241, 217, 26, 255)
love.graphics.circle("fill", 0, 0, 50, 32)
love.graphics.circle("line", 0, 0, 80, 8)
@@ -47,12 +58,6 @@ function love.draw()
love.graphics.print("Novastruct ap0.002", love.graphics.getWidth(), love.graphics.getHeight(), 0, 1, 1, 140, 18)
end
-function love.update(dt)
- ship:update(dt)
- player:update(dt)
- camera:update(dt)
-end
-
function love.mousepressed(x, y, button, istouch)
print(camera:mousePosition())
end
diff --git a/prototype/player.lua b/prototype/player.lua
index 273b032..76898fd 100644
--- a/prototype/player.lua
+++ b/prototype/player.lua
@@ -1,7 +1,7 @@
player = {}
player.worldX = 0 -- set inside the class, read only
player.worldY = 0 -- set inside the class, read only
-player.localX = 7
+player.localX = 15
player.localY = 7
player.walkSpeed = 5
player.sprintSpeed = 10
@@ -16,6 +16,7 @@ player.timeRight = 0
player.controlMode = "player"
function player:load()
+ self.doorSound = love.audio.newSource("door.ogg")
self.image = love.graphics.newImage("player.png")
self.parent = {}
end
@@ -90,42 +91,34 @@ function player:keypressed(key, scancode, isrepeat)
end
function player:moveStep(x, y)
- if self.parent.map[y+1][x+1] == 0 then
- -- space
+ local tile = self.parent.map[y+1][x+1]
- elseif self.parent.map[y+1][x+1] == 1 then
- -- wall
-
- elseif self.parent.map[y+1][x+1] == 2 then
- -- floor
+ if tile == "empty" then
+ elseif tile == "wall" then
+ elseif tile == "floor" then
self.localX = x
self.localY = y
-
- elseif self.parent.map[y+1][x+1] == 3 then
- -- flight display panel
- self.localX = x
- self.localY = y
-
- elseif self.parent.map[y+1][x+1] == 4 then
- -- flight control panel
+ elseif tile == "glass" then
+ elseif tile == "flight_control" then
self.localX = x
self.localY = y
self:pilotShip(self.parent)
-
- elseif self.parent.map[y+1][x+1] == 5 then
- -- airlock
-
- elseif self.parent.map[y+1][x+1] == 6 then
- -- fuel control panel
+ elseif tile == "door" then
self.localX = x
self.localY = y
-
- elseif self.parent.map[y+1][x+1] == 7 then
- -- fuel line
-
- elseif self.parent.map[y+1][x+1] == 8 then
- -- engine
-
+ love.audio.stop(self.doorSound)
+ love.audio.play(self.doorSound)
+ elseif tile == "fuel_control" then
+ self.parent:playerInteractFuel(x+1, y+1)
+ -- self.localX = x
+ -- self.localY = y
+ elseif tile == "fuel_line" then
+ elseif tile == "thruster" then
+ elseif tile == "weapon" then
+ elseif tile == "weapon_control" then
+ self.parent:fire(x+1, y+1)
+ -- self.localX = x
+ -- self.localY = y
end
end
diff --git a/prototype/ship.lua b/prototype/ship.lua
index a903baa..4d4ec16 100644
--- a/prototype/ship.lua
+++ b/prototype/ship.lua
@@ -16,37 +16,100 @@ ship.controlled = false
ship.dragEnabled = false
ship.rotDragEnabled = false
--- 0 = empty
--- 1 = wall
--- 2 = floor
--- 3 = flight display panel
--- 4 = flight control panel
--- 5 = airlock
--- 6 = fuel control panel
--- 7 = fuel line
--- 8 = engine
-
-ship.map = {
- {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
- {0, 0, 1, 1, 2, 2, 3, 3, 3, 2, 2, 1, 1, 0},
- {0, 1, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 1},
- {0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {5, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {5, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
- {0, 1, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 1},
- {0, 0, 1, 1, 2, 2, 7, 7, 7, 2, 2, 1, 1, 0},
- {0, 0, 0, 0, 1, 1, 1, 7, 1, 1, 1, 0, 0, 0},
- {0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 0, 0, 0, 0},
-}
+function pix_id(hex)
+ if hex == "000000" then
+ return "empty"
+ end
+ if hex == "455A64" then
+ return "wall"
+ end
+ if hex == "212121" then
+ return "floor"
+ end
+ if hex == "42A5F5" then
+ return "glass"
+ end
+ if hex == "0D47A1" then
+ return "flight_control"
+ end
+ if hex == "4A148C" then
+ return "door"
+ end
+ if hex == "004D40" then
+ return "fuel_control"
+ end
+ if hex == "64DD17" then
+ return "fuel_line"
+ end
+ if hex == "FFEA00" then
+ return "thruster"
+ end
+ if hex == "D50000" then
+ return "weapon"
+ end
+ if hex == "D32F2F" then
+ return "weapon_control"
+ end
+
+ return hex
+end
function ship:load()
self.image = love.graphics.newImage("ship.png")
+ self.map = {}
+ local imageData = self.image:getData()
+ local h = imageData:getHeight()
+ local w = imageData:getWidth()
+ for y=1, h do
+ for x=1, w do
+ -- print(x .. " " .. w .. "\n" .. y .. " " .. h)
+ r, g, b, a = imageData:getPixel(x-1, y-1)
+ hex = rgb_to_hex(r, g, b)
+ if not self.map[y] then self.map[y]={} end
+ self.map[y][x] = pix_id(hex)
+ end
+ end
+
+ self.computerbeepSound = love.audio.newSource("computerbeep.ogg")
+ self.fireSound = love.audio.newSource("fire.ogg")
+ self.thrustSound = love.audio.newSource("thrust.ogg")
+ self.thrustSound:setLooping(true)
+
+ print_r(self.map)
+end
+
+function print_r ( t )
+ local print_r_cache={}
+ local function sub_print_r(t,indent)
+ if (print_r_cache[tostring(t)]) then
+ print(indent.."*"..tostring(t))
+ else
+ print_r_cache[tostring(t)]=true
+ if (type(t)=="table") then
+ for pos,val in pairs(t) do
+ if (type(val)=="table") then
+ print(indent.."["..pos.."] => "..tostring(t).." {")
+ sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
+ print(indent..string.rep(" ",string.len(pos)+6).."}")
+ elseif (type(val)=="string") then
+ print(indent.."["..pos..'] => "'..val..'"')
+ else
+ print(indent.."["..pos.."] => "..tostring(val))
+ end
+ end
+ else
+ print(indent..tostring(t))
+ end
+ end
+ end
+ if (type(t)=="table") then
+ print(tostring(t).." {")
+ sub_print_r(t," ")
+ print("}")
+ else
+ sub_print_r(t," ")
+ end
+ print()
end
function ship:update(dt)
@@ -68,6 +131,13 @@ function ship:update(dt)
self.vx = self.vx + math.sin(self.rot) * self.thrust * dt
self.vy = self.vy - math.cos(self.rot) * self.thrust * dt
self.dragEnabled = false
+ if not self.thrustSound:isPlaying() then
+ self.thrustSound:play()
+ end
+ else
+ if self.thrustSound:isPlaying() then
+ self.thrustSound:stop()
+ end
end
end
@@ -89,3 +159,38 @@ end
function ship:draw()
love.graphics.draw(self.image, self.x, self.y, self.rot, 1, 1, self.image:getWidth() / 2, self.image:getHeight() / 2)
end
+
+function ship:playerInteractFuel()
+ self.computerbeepSound:stop()
+ self.computerbeepSound:play()
+end
+
+function ship:fire(x, y)
+ self.fireSound:stop()
+ self.fireSound:play()
+end
+
+function rgb_to_hex(r, g, b)
+ -- local hex = DEC_HEX(r) .. DEC_HEX(g) .. DEC_HEX(b)
+ local hex = string.format("%02X%02X%02X", r, g, b)
+ return hex
+end
+
+function rgba_to_hex(r, g, b, a)
+ -- local hex = DEC_HEX(r) .. DEC_HEX(g) .. DEC_HEX(b) .. DEC_HEX(a)
+ local hex = string.format("%04X%04X%04X%04X", r, g, b, a)
+ return hex
+end
+
+function DEC_HEX(IN)
+ local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
+ if IN == 0 then
+ return "00"
+ end
+ while IN>0 do
+ I=I+1
+ IN,D=math.floor(IN/B),math.mod(IN,B)+1
+ OUT=string.sub(K,D,D)..OUT
+ end
+ return OUT
+end
diff --git a/prototype/ship.png b/prototype/ship.png
index 3ab2197..108c87d 100644
--- a/prototype/ship.png
+++ b/prototype/ship.png
Binary files differ
diff --git a/prototype/stars.lua b/prototype/stars.lua
new file mode 100644
index 0000000..088def8
--- /dev/null
+++ b/prototype/stars.lua
@@ -0,0 +1,28 @@
+stars = {}
+stars.stardust = {}
+
+function stars:load()
+ for i=0, 1000 do
+ self.stardust[i] = {}
+ self.stardust[i].x = math.random(-1000, 1000)
+ self.stardust[i].y = math.random(-1000, 1000)
+ end
+end
+
+function stars:update(dt)
+ for i in ipairs(self.stardust) do
+ if camera:inBoundsWorldCoords(self.stardust[i].x, self.stardust[i].y) then
+ self.stardust[i].active = true
+ else
+ self.stardust[i].active = false
+ end
+ end
+end
+
+function stars:draw()
+ for i in ipairs(self.stardust) do
+ if self.stardust[i].active then
+ love.graphics.circle("fill", self.stardust[i].x, self.stardust[i].y, math.random(0.4,0.5), 4)
+ end
+ end
+end
diff --git a/prototype/thrust.ogg b/prototype/thrust.ogg
new file mode 100644
index 0000000..1feace3
--- /dev/null
+++ b/prototype/thrust.ogg
Binary files differ