Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 168 additions and 180 deletions.
  1. +19 −8 char.lua
  2. +2 −2 game.lua
  3. +22 −30 level.lua
  4. +125 −140 platforms.lua
View
27 char.lua
@@ -33,35 +33,46 @@ end
function Character:draw()
love.graphics.setColor(128,255,11)
- love.graphics.rectangle("fill",self.pos[1] - 5, self.pos[2] - 10, 10, 10)
+ love.graphics.rectangle("fill",self.pos[1], self.pos[2], 10, 10)
end
function Character:update(dt)
-
-- movement
self.pos = self.pos:add(self.dir:smul(self.speed * dt))
- local colRes = self.game:checkCharStatus( self.pos )
+ local colRes = self.game:checkCharStatus( self.pos, {10, 10} )
-- jump?
- if not colRes[1] and self.standing then
+ self.lastStandingPlatform = self.standingPlatform
+ self.standingPlatform = colRes[4]
+ if not colRes[1] and self.standing and self.lastStandingPlatform then
+ if self.lastStandingPlatform:moved() then
+ self.lastStandingPlatform:resetMoved()
+ else
self.dir[2] = self.jump
+ end
end
-
+
+ if self.standingPlatform and self.standingPlatform ~= self.lastStandingPlatform and self.standingPlatform:moved() then
+ self.standingPlatform:resetMoved()
+ end
+
-- correct position on collision
if colRes[1] then
self.pos = colRes[2]
- self.standing = true
+ if colRes[3][2] ~= 1 then
+ self.standing = true
+ end
else
self.standing = false
end
-- stop by the floor
if colRes[1] and colRes[3][2]~=0 then
- self.dir[2] = 0
+ self.dir[2] = 0
end
-- freefall
- if not colRes[1] then
+ if not standing then
self.dir[2] = self.dir[2]+self.grav*dt
end
View
4 game.lua
@@ -46,8 +46,8 @@ function Game:draw()
self.levels[self.current]:draw()
end
-function Game:checkCharStatus( charPos )
- return self.levels[self.current].poligons:checkCharStatus( charPos )
+function Game:checkCharStatus( charPos, charSize )
+ return self.levels[self.current].poligons:checkCharStatus( charPos, charSize )
end
View
52 level.lua
@@ -44,7 +44,7 @@ Level = class(function(self,game)
self.initpos = Vector(100,0)
self.dir = Vector(1,0)
self.game = game
- self.poligons = PlatformList()
+ self.poligons = MovablePlatformList()
end)
function Level:restart()
@@ -73,23 +73,20 @@ end
Level1 = class(Level,function(self, game)
self._base.init(self,game)
---~ self.poligons = PlatformList()
+--~ self.poligons = MovablePlatformList()
- self.poligons:append(Platform())
+ self.poligons:append(MovablePlatform())
- local tt = Platform()
- tt:set({0,0,200,0,200,20,0,20})
- tt.pos[2]=400
+ local tt = MovablePlatform()
+ tt:set({0,400,200,20})
self.poligons:append(tt)
- local t2 = Platform()
- t2:set({0,0,300,0,300,30,0,30})
- t2.pos[2]=455
- t2.pos[1]=400
+ local t2 = MovablePlatform()
+ t2:set({400,455,300,30})
self.poligons:append(t2)
- local t3 = Platform()
- t3:set({0,0,40,0,40,140,0,140})
+ local t3 = MovablePlatform()
+ t3:set({0,0,40,140})
self.poligons:append(t3)
end)
@@ -98,23 +95,20 @@ Level2 = class(Level,function(self, game)
self._base.init(self,game)
self.initpos = Vector(10,400)
self.goal.x = 100
---~ self.poligons = PlatformList()
+--~ self.poligons = MovablePlatformList()
- self.poligons:append(Platform())
+ self.poligons:append(MovablePlatform())
- local tt = Platform()
- tt:set({0,0,200,0,200,20,0,20})
- tt.pos[2]=400
+ local tt = MovablePlatform()
+ tt:set({0,400,200,20})
self.poligons:append(tt)
- local t2 = Platform()
- t2:set({0,0,300,0,300,30,0,30})
- t2.pos[2]=455
- t2.pos[1]=400
+ local t2 = MovablePlatform()
+ t2:set({400,455,300,30})
self.poligons:append(t2)
- local t3 = Platform()
- t3:set({0,0,40,0,40,140,0,140})
+ local t3 = MovablePlatform()
+ t3:set({0,0,140,40})
self.poligons:append(t3)
end)
@@ -123,14 +117,12 @@ Level3 = class(Level,function(self, game)
self._base.init(self,game)
self.initpos = Vector(500,200)
- local t1 = Platform()
- t1:set({0,0,80,0,80,250,0,250})
+ local t1 = MovablePlatform()
+ t1:set({0,0,80,250})
+ self.poligons:append(t1)
- self.poligons:append(t1)
-
-
- local t3 = Platform()
- t3:set({0,0,40,0,40,140,0,140})
+ local t3 = MovablePlatform()
+ t3:set({0,0,40,140})
self.poligons:append(t3)
end)
View
265 platforms.lua
@@ -16,172 +16,153 @@
-- You should have received a copy of the GNU General Public License
-- along with Mind The Gap If not, see <http://www.gnu.org/licenses/>.
-Platform = class(function(self)
- self.pos = Vector(200,200)
- self.points = { Vector(0,0), Vector(100,0), Vector(100,100), Vector(0,100) }
- self.minx = 0
- self.maxx = 100
- self.miny = 0
- self.maxy = 100
- self.dragging = false
- self.mouseDown = false
- self.transparent = false
+-- fixed platform: cannot be moved
+-- solid platform: you can't even move platforms across it
+
+MovablePlatform = class(function(self)
+ self.pos = Vector(200, 200)
+ self.size = Vector(100, 100)
+ self.currentPos = Vector(200, 200)
+ self.dragging = false
+ self.mouseDown = false
+ self.transparent = false
+ self.wasMoved = false
end)
-function Platform:set(ps)
- self.points = {}
- local n = math.floor(table.getn(ps)/2)
- self.minx = ps[1]
- self.maxx = ps[1]
- self.miny = ps[2]
- self.maxy = ps[2]
- local i=1
- while i<table.getn(ps) do
- local x,y = ps[i],ps[i+1]
- i = i+2
- table.insert(self.points, Vector(x,y))
- if self.minx>x then self.minx=x end
- if self.maxx<x then self.maxx=x end
- if self.miny>y then self.miny=y end
- if self.maxy<y then self.maxy=y end
- end
+function MovablePlatform:set(ps)
+ self.pos = Vector(ps[1], ps[2])
+ self.size = Vector(ps[3], ps[4])
+ self.currentPos = Vector(ps[1], ps[2])
end
-function Platform:draw()
- if self.dragging then
- love.graphics.setColor(255,55,55,128)
- else
- love.graphics.setColor(55,55,255)
- end
- love.graphics.setLineWidth(2)
- local poligonmatrix = {}
- for i=1,table.getn(self.points) do
- table.insert(poligonmatrix,self.points[i][1]+self.pos[1])
- table.insert(poligonmatrix,self.points[i][2]+self.pos[2])
- end
- love.graphics.polygon("fill",poligonmatrix)
+function MovablePlatform:draw()
+ love.graphics.setLineWidth(2)
+ if self.dragging then
+ love.graphics.setColor(255, 55, 55, 128)
+ love.graphics.rectangle("fill", self.pos[1], self.pos[2], self.size[1], self.size[2])
+ love.graphics.setColor(55, 55, 255, 128)
+ love.graphics.rectangle("fill", self.currentPos[1], self.currentPos[2], self.size[1], self.size[2])
+ else
+ love.graphics.setColor(55, 55, 255)
+ love.graphics.rectangle("fill", self.pos[1], self.pos[2], self.size[1], self.size[2])
+ end
end
-function Platform:update(dt)
- local mx,my = love.mouse.getX(), love.mouse.getY()
- local md = love.mouse.isDown("l")
- if self.dragging then
- self.pos = Vector(self.origPos[1]+mx-self.dragPos[1], self.origPos[2]+my-self.dragPos[2])
- if not md then
- self:stopDrag()
- end
- else
- if (not self.mouseDown) and md and self:containsMouse() then
- self:startDrag()
- end
+function MovablePlatform:update(dt)
+ local mx,my = love.mouse.getX(), love.mouse.getY()
+ local md = love.mouse.isDown("l")
+ if self.dragging then
+ self.currentPos = Vector(self.pos[1]+mx-self.dragPos[1], self.pos[2]+my-self.dragPos[2])
+ if not md then
+ self:stopDrag()
+ self.pos = Vector(self.currentPos[1], self.currentPos[2]);
+ end
+ else
+ if (not self.mouseDown) and md and self:containsMouse() then
+ self:startDrag()
end
- self.mouseDown = md
+ end
+ self.mouseDown = md
end
-function Platform:containsMouse()
+function MovablePlatform:containsMouse()
local mx,my = love.mouse.getX(), love.mouse.getY()
- return self:collided({mx,my}, true)
+ return self:collidedCurrent({mx,my})
end
-function Platform:startDrag()
+function MovablePlatform:startDrag()
self.dragPos = { love.mouse.getX(), love.mouse.getY() }
- self.origPos = self.pos
self.dragging = true
self.transparent = true
end
-function Platform:stopDrag()
+function MovablePlatform:stopDrag()
self.dragging = false
+ self.wasMoved = true
end
--- collision: assume all platforms are rectangles by now
-- returns if the character collides with the platform
-function Platform:collided(pos, mouse)
- if self.transparent and not mouse then return false end
- local mx,my = pos[1],pos[2]
- if mx<self.minx+self.pos[1] then return false end
- if mx>self.maxx+self.pos[1] then return false end
- if my<self.miny+self.pos[2] then return false end
- if my>self.maxy+self.pos[2] then return false end
- -- ToDo: refine this
- return true
+function MovablePlatform:collidedCurrent(pos)
+ local mx,my = pos[1],pos[2]
+ if mx < self.currentPos[1] then return false end
+ if mx > self.size[1]+self.currentPos[1] then return false end
+ if my < self.currentPos[2] then return false end
+ if my > self.size[2]+self.currentPos[2] then return false end
+ return true
end
--- returns the normal of the platform at position pos
-function Platform:normal(pos)
- local mx,my = pos[1]-self.pos[1], pos[2]-self.pos[2]
- -- top border: 10 pixels tall
- if my<=self.miny+10 then
- return Vector(0,-1)
- end
-
- -- side borders
---~ local centerx = (self.maxx+self.minx)/2
---~ if mx>centerx then
---~ return Vector(1,0)
---~ else
---~ return Vector(-1,0)
---~ end
- if mx<=self.minx+10 then
- return Vector(-1,0)
- end
-
- if mx>=self.maxx-10 then
- return Vector(1,0)
- end
-
- return Vector(0,1)
+function MovablePlatform:collidedBase(pos, size)
+ if pos[1]+size[1] < self.pos[1] then return false end
+ if pos[1] > self.size[1]+self.pos[1] then return false end
+ if pos[2]+size[2] < self.pos[2] then return false end
+ if pos[2] > self.size[2]+self.pos[2] then return false end
+ return true
+end
---~ local x = (pos[1]-self.pos[1])/(self.maxx-self.minx) - 0.5
---~ local y = (pos[2]-self.pos[2])/(self.maxy-self.miny) - 0.5
---~ if math.abs(x)>math.abs(y) then
---~ return Vector(x/math.abs(x),0)
---~ else
---~ return Vector(0,y/math.abs(y))
---~ end
+-- returns the normal of the platform at position pos
+function MovablePlatform:normal(pos)
+ local mx,my = pos[1], pos[2]
+ -- top border: 10 pixels tall
+ if my<=self.pos[2]+10 then
+ return Vector(0,-1)
+ end
+ -- side borders
+ if mx<=self.pos[1]+10 then
+ return Vector(-1,0)
+ end
+ if mx>=self.pos[1]+self.size[1]-10 then
+ return Vector(1,0)
+ end
+ return Vector(0,1)
end
-- this function checks if pos collides with the platform
-- if it does, returns { true, newpos, normal }
-- if not, returns { false, pos, 00 }
-function Platform:correctedPos( pos )
- if not self:collided( pos ) then
- if (not self.dragging) and (not self:collided( pos, true )) then
- self.transparent = false
- end
- return { false, pos, Vector(0,0) }
- else
- local n = self:normal( pos )
- local cp = Vector( pos[1], pos[2] )
- if n[1]==1 then
- cp[1] = self.maxx+self.pos[1]
- end
- if n[1]==-1 then
- cp[1] = self.minx+self.pos[1]
- end
- if n[2]==-1 then
- cp[2] = self.miny+self.pos[2]
- end
- if n[2]==1 then
- cp[2] = self.maxy+self.pos[2]
- end
- return { true, cp, n }
+function MovablePlatform:correctedPos( pos, size )
+ if not self:collidedBase( pos, size ) then
+ return { false, pos, Vector(0,0) }
+ else
+ local n = self:normal( pos )
+ local cp = Vector( pos[1], pos[2] )
+ if n[1]==1 then
+ cp[1] = self.pos[1]+self.size[1]
+ end
+ if n[1]==-1 then
+ cp[1] = self.pos[1]-size[1]
end
+ if n[2]==-1 then
+ cp[2] = self.pos[2]-size[2]
+ end
+ if n[2]==1 then
+ cp[2] = self.size[2]+self.pos[2]
+ end
+ return { true, cp, n }
+ end
+end
+
+function MovablePlatform:moved()
+ return self.wasMoved
+end
+
+function MovablePlatform:resetMoved()
+ self.wasMoved = false
end
--------------------------------------------------------------------------
-PlatformList = class(function(self)
+MovablePlatformList = class(function(self)
self.list = List()
end)
-function PlatformList:append(platform)
+function MovablePlatformList:append(platform)
self.list:pushBack(platform)
end
-function PlatformList:draw()
+function MovablePlatformList:draw()
local elem = self.list:getFirst()
while elem do
elem:draw()
@@ -189,7 +170,7 @@ function PlatformList:draw()
end
end
-function PlatformList:update(dt)
+function MovablePlatformList:update(dt)
local elem = self.list:getFirst()
while elem do
elem:update(dt)
@@ -197,21 +178,25 @@ function PlatformList:update(dt)
end
end
-function PlatformList:checkCharStatus( pos )
- local elem = self.list:getFirst()
- local newpos = pos
- local result = {}
- local newnormal = Vector(0,0)
- local endres = false
- while elem do
- result = elem:correctedPos( newpos )
- if result[1] then
- newpos = result[2]
- newnormal = newnormal:add( result[3] )
- endres = true
---~ return elem:correctedPos( pos )
- end
- elem = self.list:getNext()
+function MovablePlatformList:checkCharStatus( pos, size )
+ local elem = self.list:getFirst()
+ local newpos = pos
+ local result = {}
+ local newnormal = Vector(0,0)
+ local endres = false
+ local lastStandingPlatform = nil
+ while elem do
+ result = elem:correctedPos( newpos, size )
+ if result[1] then
+ newpos = result[2]
+ if result[3][1]==0 and result[3][2]==-1 then
+ lastStandingPlatform = elem
+ end
+ newnormal = newnormal:add( result[3] )
+ endres = true
+--~ return elem:correctedPos( pos )
end
- return {endres,newpos,newnormal}
+ elem = self.list:getNext()
+ end
+ return {endres,newpos,newnormal, lastStandingPlatform}
end

No commit comments for this range

Something went wrong with that request. Please try again.