basic stuff w movemet n collision done

This commit is contained in:
0x81c 2015-03-12 05:13:04 -04:00
parent bffa27ec01
commit 662de9407a
4 changed files with 30 additions and 18 deletions

View file

@ -2,16 +2,16 @@ Entity = class("Entity")
function Entity:initialize(t)
self.x, self.y = t.x, t.y
self.gridSize = {x = t.sizeX, y = t.sizeY}
self.size = {x = t.sizeX, y = t.sizeY}
self.color = Color:new(t.color)
self.collision = t.collision --either "none", "moveable", "unmoveable"
self.collision = t.collision --"none", "moveable", "unmoveable"
self.sprite = t.sprite
self.shaAmount = t.sha or 1
end
function Entity:atGridPos(x, y)
if x >= self.x and x <= self.x + self.gridSize.x and
y >= self.y and y <= self.gridSize.y then
-- print(x, y, self.x, self.y, self.x + self.size.x, self.y + self.size.y)
if x >= self.x and y >= self.y and x < self.x + self.size.y and y < self.y + self.size.y then
return true
end
return false
@ -29,14 +29,20 @@ function Entity:getDrawPos()
}
end
function Entity:tick()
end
function Entity:draw()
love.graphics.setColor(self.color:set())
local p = self:getDrawPos()
love.graphics.draw(self.sprite, p.x + self:sha(), p.y + self:sha(), 0, drawScale, drawScale)
end
function Entity:isAlignedWithPlayerMovement(axis, pos)
if self:getGridPos()[axis] == pos then
function Entity:isAlignedWithPlayerMovement(axis, pos, direction)
-- print(axis, pos[axis], self:getGridPos()[axis], self:getGridPos()[axis] + self.size[axis])
if self:getGridPos()[axis] <= pos[axis] and self:getGridPos()[axis] + self.size[axis] > pos[axis] then
print("true!!")
return true
end
return false

View file

@ -123,4 +123,6 @@ function loadSprites()
green = love.graphics.newImage("art/wall_g.png"),
blue = love.graphics.newImage("art/wall_b.png")
}
artifactSprite = love.graphics.newImage("art/process/cone.png")
end

View file

@ -7,8 +7,8 @@ function Player:initialize(x, y, color)
Entity.initialize(self, {
x = x,
y = y,
sizeX = 3,
sizeY = 3,
sizeX = 1,
sizeY = 1,
color = color,
collision = "none", --player can't push other player
sprite = playerSprite

View file

@ -3,6 +3,7 @@ Room = class("Room")
require "player"
require "box"
require "wall"
require "artifact"
function Room:initialize(t)
self.exits = t.exits
@ -23,18 +24,21 @@ function Room:initialize(t)
self.activeColors[color] = true
end
for i=0, 50 do
for i=0, 10 do
local c = randomColor()
local x, y = math.random(1, gridWidth), math.random(1, gridHeight)
self.collideables[c]["b: " .. x .. ", " .. y] = Box:new(x, y, c)
end
for i=0, 50 do
for i=0, 10 do
local c = randomColor()
local x, y = math.random(1, gridWidth), math.random(1, gridHeight)
self.collideables[c]["w: " .. x .. ", " .. y] = Wall:new(x, y, c)
end
self.collideables["green"]["fpp"] = Artifact:new(6, 6, "green")
end
@ -106,7 +110,7 @@ function Room:getAdjacentEntities(entities, pos, axis, direction)
--first we gather all entities in direction of movement
for _, entity in pairs(entities) do
if axis == "x" then
if entity:isAlignedWithPlayerMovement("y", pos.y) then
if entity:isAlignedWithPlayerMovement("y", pos, direction) then
if direction > 0 and entity:getGridPos()[axis] > pos[axis] then
table.insert(t, entity)
elseif direction < 0 and entity:getGridPos()[axis] < pos[axis] then
@ -114,7 +118,7 @@ function Room:getAdjacentEntities(entities, pos, axis, direction)
end
end
elseif axis == "y" then
if entity:isAlignedWithPlayerMovement("x", pos.x) then
if entity:isAlignedWithPlayerMovement("x", pos, direction) then
if direction > 0 and entity:getGridPos()[axis] > pos[axis] then
table.insert(t, entity)
elseif direction < 0 and entity:getGridPos()[axis] < pos[axis] then
@ -139,14 +143,12 @@ end
function Room:checkAndPull(entities, pos, axis, direction)
for _, entity in pairs(entities) do
local entPos = entity:getGridPos()
print(pos.x, pos.y, entPos.x, entPos.y)
if axis == "x" and entPos.x == pos.x - direction and entPos.y == pos.y then
if axis == "x" and entity:atGridPos(pos.x - direction, pos.y) then
if entity:canMove() then
entity:move(axis, direction)
return
end
elseif axis == "y" and entPos.x == pos.x and entPos.y == pos.y - direction then
elseif axis == "y" and entity:atGridPos(pos.x, pos.y - direction) then
if entity:canMove() then
entity:move(axis, direction)
return
@ -160,14 +162,16 @@ function Room:tryPlayerMove(entities, pos, axis, direction)
local entitiesToMove = {}
for _, entity in ipairs(entities) do
if entity:getGridPos()[axis] == pos[axis] + counter then
pos[axis] = pos[axis] + direction
if entity:atGridPos(pos.x, pos.y) then
print("HOW????")
if entity:canMove() then
table.insert(entitiesToMove, entity)
else
return false
end
else
break
-- break
end
counter = counter + direction
end