Skip to content

Commit

Permalink
Lots of changes
Browse files Browse the repository at this point in the history
I changed HealthHud to reflect whether the hero can currently "use"

I changed Actor to take over the majority of the responsibilities of
Hero and Met. All position and movement stuff is now in Actor, as well
as drawing and some collision.

Small changes elsewhere
  • Loading branch information
Aaron Stewart committed Jun 21, 2013
1 parent af40103 commit 07a2d5c
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 406 deletions.
6 changes: 6 additions & 0 deletions HealthHud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function HealthHud:draw()
self.healthup.width, self.healthup.height)
love.graphics.setColor(255,255,255,255)
love.graphics.print("Add a life!", self.healthup.x, self.healthup.y+5)

--Usable
local shape = self.hero:canUse()
if (shape) then
love.graphics.print("Press 'e' to use", 20, 40)
end
end

function HUD:mousePressed(x, y, button)
Expand Down
211 changes: 43 additions & 168 deletions Hero.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
local Actor = require "class/Actor"

local Hero = Actor:new()
local ImgMgr = require("mgr/ImgMgr")

function Hero:new(o)
o = o or {} -- create object if user does not provide one
Expand All @@ -11,156 +10,66 @@ local ImgMgr = require("mgr/ImgMgr")
return o
end

function Hero:init(...)
Actor.init(self)
self.collider = arg[1]

local x = arg[2]
local y = arg[3]
self.shape = self.collider:addRectangle(x,y,16,16)
self.shape.parent = self
ImgMgr:loadImage("hero", "res/graphics/ogmo.png")

self.velocity = {x = 0, y = 1}

function Hero:init(level, position)
local image_info = {}
image_info.key = "hero"
Actor.init(self, level, image_info, position, {width=16, height=16})


self.max_speed_x = 500
self.gravity = 512
self.ax = 300
self.max_velocity.x = 500
self.max_velocity.y = 3000
self.acceleration.x = 300
self.onDeath_callback = nil

self:setMaxHealth(200)
self:setHealth(200)

self:setLives(4)

--the direction the *user* is telling the
--hero to move
self.move_dir = 0


self.shape.coll_class = "hero"
self.shape.collide = function(a,b,c,d,e,f) self:collide(b,c,d,e,f) end
end

function Hero:draw()
local x,y = self.shape:center()
ImgMgr:draw("hero", x,y,0,1,1,8,8)
-- self.shape:draw("fill")
end

function Hero:update(dt)

local dx = 0
local dy = 0

if (self.move_dir == -1 or self.move_dir == 1) then
self:accelerate(self.move_dir * self.ax*dt,0)
else
if ( self:getXVelocity() == 0 ) then
elseif (math.abs(self:getXVelocity()) < self.ax*dt) then
self:setXVelocity(0)
else
self:accelerate(-1 * self:xDir() * (self.ax * dt),0)
end
end

self:capXSpeed()

dx = self:getXVelocity() * dt

-- if self:getYVelocity() ~= 0 then
dy = self:getYVelocity()*dt
self:accelerate(0,self.gravity*dt)
-- end

self:move(dx, dy)

self.coll_class = "hero"
end

------------HERO ACTIONS---------------

-----------Jump stuff
function Hero:jump()
if (self.can_jump) then
if (self:canJump()) then
self:setYVelocity(-300 - 0.25 * math.abs(self:getXVelocity()))
self.can_jump = false
end
end

function Hero:moveDir(dir)
self.move_dir = dir
end

function Hero:getMoveDir()
return self.move_dir
end

---------------POSITION/VELOCITY----------------

function Hero:getXPosition()
local x,y = self.shape:center()
return x
end

function Hero:getYPosition()
local x,y = self.shape:center()
return y
end

function Hero:getXVelocity()
return self.velocity.x
end

function Hero:getYVelocity()
return self.velocity.y
end

function Hero:move(dx, dy)
self.shape:move(dx,dy)
end


function Hero:setXPosition(x)
local xx,yy = self:getPosition()
self.shape:moveTo(x,yy)
end
function Hero:setYPosition(y)
local xx,yy = self:getPosition()
self.shape:moveTo(xx,y)
end

function Hero:setPosition(x,y)
self.shape:moveTo(x,y)
end

function Hero:xDir()
if (self.velocity.x == 0) then
return 0
elseif (self.velocity.x > 0) then
return 1
else
return -1
function Hero:canJump()
local c = self.level:getCollider()
local x,y = self:getPosition()
local shapes = c:shapesAt(x,y+10)
for _, shape in ipairs(shapes) do
if (self.checkProp(shape, "solid")) then return true end
end
end

function Hero:accelerate(dvx,dvy)
self.velocity.x = self.velocity.x + dvx
self.velocity.y = self.velocity.y + dvy
self:capXSpeed()
end


function Hero:setXVelocity(vx)
self.velocity.x = vx
self:capXSpeed()
end

function Hero:setYVelocity(vy)
self.velocity.y = vy
-------------Use stuff
function Hero:canUse()
local c = self.level:getCollider()
local x,y = self:getPosition()
local shapes = c:shapesAt(x,y)
for _, shape in ipairs(shapes) do
if (self.checkProp(shape, "usable")) then
return shape
end
end

x = x + 16*self:getFacing()
shapes = c:shapesAt(x,y)
for _, shape in ipairs(shapes) do
if (self.checkProp(shape, "usable")) then
return shape
end
end
end

function Hero:capXSpeed()
if ( math.abs(self.velocity.x) > self.max_speed_x) then
self.velocity.x = self:xDir() * self.max_speed_x
function Hero:use()
local shape = self:canUse()
if (shape) then
shape.properties.usable(self)
end
end

Expand Down Expand Up @@ -194,40 +103,16 @@ local ImgMgr = require("mgr/ImgMgr")
end

-----------------COLLISION-------------------
function Hero:collideWithSolid(dt, shape_a, shape_b, dx, dy)

if (shape_a == self.shape) then
elseif (shape_b == self.shape) then
shape_a, shape_b = shape_b, shape_a
dx, dy = -1*dx, -1*dy
else
return
end


-- collision hero entites with level geometry
shape_a:move(dx*1.1, dy*1.1)

if (dx ~= 0) then
self:setXVelocity(0)
end

if (dy ~= 0) then
self:setYVelocity(0)
self.can_jump = (dy < 0)
end

end

function Hero:collide(dt, me, them, dx, dy)

if (them.coll_class ~= nil and them.coll_class == "prop_tile") then
if (them.properties.hurty) then self:ouch(dx,dy) end
end

if (self.checkProp(them, "solid")) then
self:collideWithSolid(dt, me, them, dx, dy)
elseif (them.coll_class == "met") then
Actor.collide(self, dt, me, them, dx, dy)

if (them.coll_class == "met") then
cx,cy = self.diff(me,them)
self:setYVelocity(-200)
if(cx > 0) then self:setXVelocity(-250)
Expand All @@ -242,16 +127,6 @@ local ImgMgr = require("mgr/ImgMgr")
end
end

function Hero.checkProp(them, prop)
return ((them.properties ~= nil) and (them.properties[prop]))
end

function Hero:endCollideWithSolid(dt, shape_a, shape_b)
if self:getYVelocity() == 0 then
--self:setYVelocity(1)
end
end

function Hero:ouch(dx, dy)
self:setYVelocity(-150)
self:damage(17)
Expand Down
Loading

0 comments on commit 07a2d5c

Please sign in to comment.