Skip to content

Commit

Permalink
Keep ship inside of play area. (Fixes #13)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAPerson committed Aug 26, 2012
1 parent 3d32096 commit a5f49f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
33 changes: 15 additions & 18 deletions ship.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,29 @@ end
-- handles various collision related events.
-- mostly simply handles self Vs. Level terrain collision atm.
function ship:docollision(level, dt) -- this is new
local CurrentTileX = round(self.pos_x / 32 + level.x / 32, 0)

-- Keep ship inside play area
local posy = self.pos_y
posy = posy <= 32 and 32 or posy
posy = posy + self.height >= level.height*32 and
level.height*32 - self.height or posy
self.pos_y = posy

local posx = self.pos_x
posx = posx <= 0 and 0 or posx
posx = posx + self.width >= 800 and 800 - self.width or posx
self.pos_x = posx

local CurrentTileX = round(posx / 32 + level.x / 32, 0)
if CurrentTileX > 0 and CurrentTileX < level.width then
local testtile = math.max(level.data[CurrentTileX], level.data[CurrentTileX+1])
if self.pos_y - self.height < testtile * 32 then
if posy - self.height < testtile * 32 then
self:dohit(dt*10)
self.pos_y = (testtile * 32) + self.height
elseif self.pos_y + self.height > (level.height - testtile) * 32 then
local ny =
elseif posy + self.height > (level.height - testtile) * 32 then
self:dohit(dt*10)
self.pos_y = ((level.height - testtile) * 32) - self.height
end

-- TODO: Add test to constrain the ship to height/depth of the *level*

if self.pos_y + self.height >= SCREEN_HEIGHT-self.height then
self.pos_y = SCREEN_HEIGHT-self.height
elseif self.pos_y <= self.height then
self.pos_y = self.height
end

if self.pos_x >= SCREEN_WIDTH-self.radius then
self.pos_x = SCREEN_WIDTH-self.radius
elseif self.pos_x <= self.radius then
self.pos_x = self.radius
end
end
end

Expand Down
3 changes: 0 additions & 3 deletions space.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ local GUI_GradientBar = love.graphics.newImage("gfx/GUI_GradientBar.png")

state.player = Ship.new {name = 'player';
texture = spaceship;
height = 19;
npc = false;
}

Expand Down Expand Up @@ -86,7 +85,6 @@ function state:enter()
pos_x = math.random(0, 800);
pos_y = math.random(0, 600);
texture = Enemy2;
height = 32;
}
end
end
Expand All @@ -111,7 +109,6 @@ function state:update(dt)
pos_x = math.random(0, 800/2);
pos_y = math.random(0, 600/2);
texture = Enemy2;
height = 32;
})
end

Expand Down

0 comments on commit a5f49f3

Please sign in to comment.