Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extracted stuff into Level class, made player gravitate to grid-align…

…ed movement.
  • Loading branch information...
commit 59fbdf18e5d5a73e4d8e3ce56a8eaa51629e52bc 1 parent 9316dc6
Ross Andrews authored
Showing with 82 additions and 47 deletions.
  1. +58 −0 level.lua
  2. +23 −46 main.lua
  3. +1 −1  map.lua
View
58 level.lua
@@ -0,0 +1,58 @@
+module(..., package.seeall)
+
+local Map = require('map')
+
+methods = setmetatable({}, {__index=Map.methods})
+local SIZE = 32
+
+function new(strs)
+ local tbl = Map.new_from_strings(strs)
+ getmetatable(tbl).__index = methods
+ tbl:init()
+ return tbl
+end
+
+function methods:init()
+end
+
+function methods:makeEdges(world)
+ assert(world)
+
+ local w = SIZE * self.width
+ local h = SIZE * self.height
+
+ local edge = {}
+ edge.body = love.physics.newBody(world, 0, 0, 'static')
+ edge.shapes = {}
+ table.insert(edge.shapes, love.physics.newEdgeShape(0, 0, w, 0))
+ table.insert(edge.shapes, love.physics.newEdgeShape(0, 0, 0, h))
+ table.insert(edge.shapes, love.physics.newEdgeShape(w, h, w, 0))
+ table.insert(edge.shapes, love.physics.newEdgeShape(w, h, 0, h))
+ for _, s in ipairs(edge.shapes) do love.physics.newFixture(edge.body, s) end
+ return edge
+end
+
+function methods:makeWalls(world)
+ local ph = love.physics
+ local walls = ph.newBody(world, 0, 0, 'static')
+
+ for p in self:each() do
+ if self(p) == '#' then
+ local s = ph.newRectangleShape(p.x*SIZE + SIZE/2, p.y*SIZE + SIZE/2,
+ SIZE, SIZE)
+ ph.newFixture(walls, s)
+ end
+ end
+end
+
+function methods:makePlayer(world)
+ local player = {}
+ local ph = love.physics
+
+ player.body = ph.newBody(world, SIZE*1.5, SIZE*1.5, 'dynamic')
+ player.shape = ph.newCircleShape(10)
+ ph.newFixture(player.body, player.shape)
+
+ player.body:setMass(0.01)
+ return player
+end
View
69 main.lua
@@ -1,6 +1,6 @@
assert(love, "Run this inside Love")
-local Map = require('map')
+local Level = require('level')
local point = require('point')
local world = nil
@@ -8,7 +8,7 @@ local world = nil
local edge = nil
local player = nil
-local level = Map.new_from_strings{
+local level = Level.new{
"..........######.........",
"..........#..............",
"..........#.#............",
@@ -23,6 +23,7 @@ local level = Map.new_from_strings{
"...#...#...#........#....",
".....#...#...............",
".........................",
+ ".........................",
"........................."
}
@@ -31,50 +32,10 @@ function love.load()
love.physics.setMeter(32)
world = love.physics.newWorld(0, 0)
- edge = makeEdges(world,
- 0, 0,
- level.width*32, level.height*32)
-
- makeWalls(world, level, 32)
- player = makePlayer(world)
-end
-function makeEdges(world, x, y, w, h)
- local edge = {}
- edge.body = love.physics.newBody(world, 0, 0, 'static')
- edge.shapes = {}
- table.insert(edge.shapes, love.physics.newEdgeShape(x, y, x+w, y))
- table.insert(edge.shapes, love.physics.newEdgeShape(x, y, x, y+h))
- table.insert(edge.shapes, love.physics.newEdgeShape(x+w, y+h, x+w, y))
- table.insert(edge.shapes, love.physics.newEdgeShape(x+w, y+h, x, y+h))
- for _, s in ipairs(edge.shapes) do love.physics.newFixture(edge.body, s) end
- return edge
-end
-
-function makeWalls(world, level, size)
- local ph = love.physics
- size = size or 32
- local walls = ph.newBody(world, 0, 0, 'static')
-
- for p in level:each() do
- if level(p) == '#' then
- local s = ph.newRectangleShape(p.x*size + size/2, p.y*size + size/2,
- size, size)
- ph.newFixture(walls, s)
- end
- end
-end
-
-function makePlayer(world)
- local player = {}
- local ph = love.physics
-
- player.body = ph.newBody(world, 32, 32, 'dynamic')
- player.shape = ph.newCircleShape(10)
- ph.newFixture(player.body, player.shape)
-
- player.body:setMass(0.01)
- return player
+ edge = level:makeEdges(world)
+ level:makeWalls(world)
+ player = level:makePlayer(world)
end
function love.draw()
@@ -130,11 +91,27 @@ function love.update(dt)
player.body:setLinearDamping(15)
end
- if kd == 1 then player.direction = dir end
player.location = point(
math.floor(player.body:getX() / 32),
math.floor(player.body:getY() / 32))
+ if kd == 1 then
+ player.direction = dir
+ local acc = 25
+
+ if player.direction.y == 0 then -- horizontal
+ local y = player.body:getY()-16
+ local ty = player.location.y * 32
+ local f = dt * acc * (ty - y)
+ player.body:applyForce(0, f)
+ else -- vertical
+ local x = player.body:getX()-16
+ local tx = player.location.x * 32
+ local f = dt * acc * (tx - x)
+ player.body:applyForce(f, 0)
+ end
+ end
+
max_speed(player.body, 300)
world:update(dt)
end
View
2  map.lua
@@ -2,7 +2,7 @@ module(..., package.seeall)
local point = require('point')
-local methods = {}
+methods = {}
function new(w, h)
local tbl = {
Please sign in to comment.
Something went wrong with that request. Please try again.