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) function Entity:initialize(t)
self.x, self.y = t.x, t.y 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.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.sprite = t.sprite
self.shaAmount = t.sha or 1 self.shaAmount = t.sha or 1
end end
function Entity:atGridPos(x, y) function Entity:atGridPos(x, y)
if x >= self.x and x <= self.x + self.gridSize.x and -- print(x, y, self.x, self.y, self.x + self.size.x, self.y + self.size.y)
y >= self.y and y <= self.gridSize.y then 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 return true
end end
return false return false
@ -29,14 +29,20 @@ function Entity:getDrawPos()
} }
end end
function Entity:tick()
end
function Entity:draw() function Entity:draw()
love.graphics.setColor(self.color:set()) love.graphics.setColor(self.color:set())
local p = self:getDrawPos() local p = self:getDrawPos()
love.graphics.draw(self.sprite, p.x + self:sha(), p.y + self:sha(), 0, drawScale, drawScale) love.graphics.draw(self.sprite, p.x + self:sha(), p.y + self:sha(), 0, drawScale, drawScale)
end end
function Entity:isAlignedWithPlayerMovement(axis, pos) function Entity:isAlignedWithPlayerMovement(axis, pos, direction)
if self:getGridPos()[axis] == pos then -- 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 return true
end end
return false return false

View file

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

View file

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

View file

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