diff --git a/game/main.lua b/game/main.lua index 1e38d95..f4f764d 100644 --- a/game/main.lua +++ b/game/main.lua @@ -14,6 +14,8 @@ local mapX = 0 local mapY = 0 local mapSpeed = 32 +local someUnit + local function drawText(str, x, y) local font = love.graphics.getFont() love.graphics.print(str, x - font:getWidth(str) / 2, y - font:getHeight() / 2) @@ -48,6 +50,8 @@ function love.load() ) tilemap:random(10, 10) + require "unit" + someUnit = SomeUnit:new(tilemap, 30, 30) end -- 0: nothing @@ -117,13 +121,17 @@ function love.draw() -- tilemap.offsetX = 50 + math.floor(math.cos(time) * 25) -- tilemap.offsetY = 50 + math.floor(math.sin(time) * 25) tilemap:update() - local tilemapPosX = tilemap.offsetX - local tilemapPosY = tilemap.offsetY + -- local tilemapPosX = tilemap.offsetX + -- local tilemapPosY = tilemap.offsetY + local tilemapPosX = 10 + local tilemapPosY = 10 tilemap:draw(tilemapPosX, tilemapPosY) love.graphics.setColor(255, 0, 0) love.graphics.rectangle("line", tilemapPosX - 1, tilemapPosY - 1, 16 * 5 + 1, 16 * 5 + 1) love.graphics.setColor(255, 255, 255) + someUnit:draw() + drawText('Hellorld!', 160, 100) if mouseState == 2 then diff --git a/game/res/unit.png b/game/res/unit.png new file mode 100644 index 0000000..a41a33e Binary files /dev/null and b/game/res/unit.png differ diff --git a/game/unit.lua b/game/unit.lua new file mode 100644 index 0000000..0dadbb0 --- /dev/null +++ b/game/unit.lua @@ -0,0 +1,50 @@ +BaseUnit = {} +BaseUnit.__index = BaseUnit + +function BaseUnit:new(tilemap, x, y, size) + local this = {} + this.tilemap = tilemap + this.x = x + this.y = y + this.size = size or 16 + + this.sizeHalf = math.floor(this.size / 2) -- TODO: replace with Lua 5.3's integer thingies + return setmetatable(this, BaseUnit) +end + +function BaseUnit:middlePoint() + return self.x + self.sizeHalf, self.y + self.sizeHalf +end + +function BaseUnit:command(x, y, entity) + -- NOOP +end + +function BaseUnit:draw() + -- NOOP +end + +function BaseUnit:drawLimited(quad) + -- NOOP +end + +function BaseUnit:__tostring() + return "BaseUnit(x=" .. self.x .. ", y=" .. self.y .. ")" +end + +---------------------- + +SomeUnit = setmetatable({}, { __index = BaseUnit }) +SomeUnit.__index = SomeUnit + +local unitTexture = love.graphics.newImage("res/unit.png") + +function SomeUnit:new(tilemap, x, y) + local this = BaseUnit:new(tilemap, x, y, 16) + + return setmetatable(this, SomeUnit) +end + +function SomeUnit:draw() + love.graphics.draw(unitTexture, self.x - self.tilemap.offsetX, self.y - self.tilemap.offsetY) +end