summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2016-03-20 16:23:56 +0100
committerGravatar alecdwm 2016-03-20 16:23:56 +0100
commitaeca29dd19b514ce105b45067d8c84d274b28845 (patch)
treee83ca9ee403e0a754797c89e02f4d13ad1f69a59
parent500bec44f5df12415a0bb5370f71425df7b50096 (diff)
Cleaned up ship.lua and added more stars
-rw-r--r--prototype/lib.lua41
-rw-r--r--prototype/main.lua2
-rw-r--r--prototype/ship.lua119
-rw-r--r--prototype/stars.lua2
-rw-r--r--prototype/tiles.lua39
5 files changed, 94 insertions, 109 deletions
diff --git a/prototype/lib.lua b/prototype/lib.lua
new file mode 100644
index 0000000..3e44a58
--- /dev/null
+++ b/prototype/lib.lua
@@ -0,0 +1,41 @@
+function rgb_to_hex(r, g, b)
+ return string.format("%02X%02X%02X", r, g, b)
+end
+
+function rgba_to_hex(r, g, b, a)
+ return string.format("%02X%02X%02X%02X", r, g, b, a)
+end
+
+function print_table(t)
+ local print_table_cache={}
+ local function sub_print_table(t, indent)
+ if (print_table_cache[tostring(t)]) then
+ print(indent.."*"..tostring(t))
+ else
+ print_table_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_table(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_table(t," ")
+ print("}")
+ else
+ sub_print_table(t," ")
+ end
+ print()
+end
diff --git a/prototype/main.lua b/prototype/main.lua
index 5adc807..41b6121 100644
--- a/prototype/main.lua
+++ b/prototype/main.lua
@@ -1,3 +1,5 @@
+require "lib"
+require "tiles"
require "camera"
require "player"
require "ship"
diff --git a/prototype/ship.lua b/prototype/ship.lua
index 4d4ec16..2b84a59 100644
--- a/prototype/ship.lua
+++ b/prototype/ship.lua
@@ -16,100 +16,28 @@ ship.controlled = false
ship.dragEnabled = false
ship.rotDragEnabled = false
-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()
+ -- load images
self.image = love.graphics.newImage("ship.png")
+
+ -- load sounds
+ 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)
+
+ -- construct map
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)
+ r, g, b, a = imageData:getPixel(x-1, y-1)
+ self.map[y][x] = tiles:getTileTypeFromColor(rgb_to_hex(r, g, b))
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)
@@ -169,28 +97,3 @@ 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/stars.lua b/prototype/stars.lua
index 088def8..87980b0 100644
--- a/prototype/stars.lua
+++ b/prototype/stars.lua
@@ -2,7 +2,7 @@ stars = {}
stars.stardust = {}
function stars:load()
- for i=0, 1000 do
+ for i=0, 5000 do
self.stardust[i] = {}
self.stardust[i].x = math.random(-1000, 1000)
self.stardust[i].y = math.random(-1000, 1000)
diff --git a/prototype/tiles.lua b/prototype/tiles.lua
new file mode 100644
index 0000000..c822f9b
--- /dev/null
+++ b/prototype/tiles.lua
@@ -0,0 +1,39 @@
+tiles = {}
+
+function tiles:getTileTypeFromColor(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