Skip to content

Commit

Permalink
Added Face class.
Browse files Browse the repository at this point in the history
  • Loading branch information
wkiri committed Dec 7, 2011
1 parent 881866d commit e4421a4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
7 changes: 6 additions & 1 deletion construct.lua
Expand Up @@ -2,6 +2,7 @@ require "middleclass" -- definition of useful class construction methods
require 'constants'
require 'spob'
require 'segment'
require 'face'
local matrix = require 'matrix_utils'

-- Construct class: Spob composed of multiple Segments
Expand Down Expand Up @@ -65,7 +66,11 @@ function Construct:initialize(
local p7 = matrix{ stop2.x, stop2.y, -half_height }
local p8 = matrix{ start2.x, start2.y, -half_height }

local faces = { { p1, p2, p3, p4 }, {p5, p6, p7, p8} }
-- All rings are red-type on the inside, blue-type on the outside.
-- Todo: generalize this to be information about type that is passed in
-- to the Construct constructor.
local faces = { Face:new({p1, p2, p3, p4}, {R = 220, G = 100, B = 100}),
Face:new({p5, p6, p7, p8}, {R = 100, G = 100, B = 220}) }
segment = Segment:new(self, faces)
segment:rotateInitialFaces(rot_matrix)

Expand Down
14 changes: 14 additions & 0 deletions face.lua
@@ -0,0 +1,14 @@
require 'middleclass'
require 'spob'

Face = class('Face')

-- For now, pass in desired properties of each face.
-- Ultimately we will probably subclass this to
-- represent different aspects/behavior for different types
-- (habitation, solar collection, mining, factory, computing...)
function Face:initialize(coords, color)
self.coords = coords
self.color = color
end

6 changes: 5 additions & 1 deletion main.lua
Expand Up @@ -11,8 +11,10 @@ require 'fps'
require 'preferences'
require 'text'
require 'help'
-- require 'profiler'

function love.load()
-- profiler.start()
initializeScreen()
scale = ScreenScale:new()
scale.screen_scale = 0.6e9
Expand Down Expand Up @@ -50,7 +52,9 @@ function love.keypressed(key, unicode)
elseif key == 'up' then scale:zoomIn(KEYBOARD_ZOOM_FACTOR)
elseif key == 'down' then scale:zoomOut(KEYBOARD_ZOOM_FACTOR)
elseif key == 'f' then toggleFullscreen()
elseif key == 'q' then love.event.push('q')
elseif key == 'q' then
-- profiler.stop()
love.event.push('q')
elseif key == ' ' then preferences.toggle('pause_time')
elseif key == 'p' then preferences.toggle('enlarge_planets')
elseif key == 'o' then preferences.toggle('show_orbits')
Expand Down
20 changes: 10 additions & 10 deletions paintable_construct.lua
Expand Up @@ -22,7 +22,7 @@ function PaintableConstruct:draw()


local polygon = {}
local normal = matrix.unit_cross(face[1],face[2],face[3])
local normal = matrix.unit_cross(face.coords[1],face.coords[2],face.coords[3])

local view_facingness = matrix.dot(normal, VIEW_AXIS)

Expand All @@ -39,25 +39,25 @@ function PaintableConstruct:draw()
end
local color_scale = (0.4 + 0.6 * view_facingness) * illumination

for _, point in pairs(face) do
for _, point in pairs(face.coords) do
local screen_x, screen_y = scale:screenCoords(point + construct_location)
table.insert(polygon, screen_x)
table.insert(polygon, screen_y)
end
-- Need line mode to be able to see edge-on polygons!

love.graphics.setColor(
self.color.R * color_scale,
self.color.G * color_scale,
self.color.B * color_scale
face.color.R * color_scale,
face.color.G * color_scale,
face.color.B * color_scale
)

love.graphics.polygon("fill", polygon)
local wireframe_scale = math.max(0.8 * color_scale, 0.3)
love.graphics.setColor(
self.color.R * wireframe_scale,
self.color.G * wireframe_scale,
self.color.B * wireframe_scale
face.color.R * wireframe_scale,
face.color.G * wireframe_scale,
face.color.B * wireframe_scale
)
love.graphics.polygon("line", polygon)
end
Expand All @@ -68,8 +68,8 @@ end

function PaintableConstruct:faceCenter(face)
local sum = matrix:new{0,0,0}
for _, point in pairs(face) do
for _, point in pairs(face.coords) do
sum = sum + point
end
return sum / #face
return sum / #face.coords
end
5 changes: 3 additions & 2 deletions segment.lua
@@ -1,6 +1,7 @@
require 'constants'
require 'middleclass'
require "spob"
require 'face'

local matrix = require 'matrix_utils'

Expand All @@ -25,10 +26,10 @@ function Segment:rotateFaces(rot_matrix)
local result_faces = {}
for face_i, face in pairs(self.initial_faces) do
local rotated_face = {}
for point_i, point in pairs(face) do
for point_i, point in pairs(face.coords) do
rotated_face[point_i] = rot_matrix * point
end
result_faces[face_i] = rotated_face
result_faces[face_i] = Face:new(rotated_face, face.color)
end
return result_faces
end
Expand Down

0 comments on commit e4421a4

Please sign in to comment.