Первый коммит игры, карта тайлов

This commit is contained in:
Иван Кузьменко 2025-09-05 22:41:25 +03:00
parent eff0fe1b23
commit 265dd00213
10 changed files with 132 additions and 13 deletions

View file

@ -1,4 +1,4 @@
# LÖVE DOS/Native Abstraction
# DOSember Jam 2025 Entry by Tea Sanctuary
## Setup

3
game/conf.lua Normal file
View file

@ -0,0 +1,3 @@
function love.conf(t)
t.console = true
end

View file

@ -1,12 +1,18 @@
if love.system.getOS() ~= 'DOS' then
local lick = require "3rd.lick"
lick.updateAllFiles = true
-- local lick = require "3rd.lick"
-- lick.updateAllFiles = true
end
local platform = require "platform.platform"
local ttf
require "tilemap"
local bf
local hamsterMouse
local tilemap
local mapX = 0
local mapY = 0
local mapSpeed = 32
local function drawText(str, x, y)
local font = love.graphics.getFont()
@ -16,15 +22,42 @@ end
function love.load()
platform.init()
ttf = love.graphics.newFont()
bf = love.graphics.newImageFont("font.png",
bf = love.graphics.newImageFont("res/font.png",
" abcdefghijklmnopqrstuvwxyz" ..
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0" ..
"123456789.,!?-+/():;%&`'*#=[]\"")
if love.system.getOS() == 'DOS' then
hamsterMouse = love.graphics.newImage("hammouse.gif")
hamsterMouse = love.graphics.newImage("res/hammouse.gif")
else
hamsterMouse = love.graphics.newImage("hammouse.png")
hamsterMouse = love.graphics.newImage("res/hammouse.png")
end
tilemap = TileMap:new(
16,
-- TODO: replace the Kenney tiles
{
love.graphics.newImage("res/kenney/t0000.png"),
love.graphics.newImage("res/kenney/t0001.png"),
love.graphics.newImage("res/kenney/t0002.png")
},
{},
platform.isNative and "replace" or "fast",
16 * 5
)
tilemap:random(10, 10)
end
function love.update(dt)
if love.keyboard.isDown("d") then
mapX = mapX + mapSpeed * dt
elseif love.keyboard.isDown("a") then
mapX = mapX - mapSpeed * dt
end
if love.keyboard.isDown("w") then
mapY = mapY - mapSpeed * dt
elseif love.keyboard.isDown("s") then
mapY = mapY + mapSpeed * dt
end
end
@ -32,12 +65,20 @@ function love.draw()
platform.drawStart()
love.graphics.clear()
love.graphics.setFont(ttf)
drawText('Hellorld!', 160, 100)
love.graphics.setFont(bf)
love.graphics.print(" abcdefghijklmnopqrstuvwxyz", 0, 110)
love.graphics.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ0", 0, 130)
love.graphics.print("123456789.,!?-+/():;%&`'*#=[]\"", 0, 150)
-- local time = love.timer.getTime()
tilemap.offsetX = math.floor(mapX) -- 50 + math.floor(math.cos(time) * 25)
tilemap.offsetY = math.floor(mapY) -- 50 + math.floor(math.sin(time) * 25)
tilemap:update()
local tilemapPosX = tilemap.offsetX
local tilemapPosY = tilemap.offsetY
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)
drawText('Hellorld!', 160, 100)
love.graphics.draw(hamsterMouse, love.mouse.getX(), love.mouse.getY())

View file

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 219 B

After

Width:  |  Height:  |  Size: 219 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 329 B

After

Width:  |  Height:  |  Size: 329 B

Before After
Before After

BIN
game/res/kenney/t0000.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

BIN
game/res/kenney/t0001.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 B

BIN
game/res/kenney/t0002.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 B

75
game/tilemap.lua Normal file
View file

@ -0,0 +1,75 @@
TileMap = {}
TileMap.__index = TileMap
function TileMap:new(size, tiles, map, blendMode, canvasWidth, canvasHeight)
local this = {}
this.size = size
this.tiles = tiles or {}
this.map = map or {}
this.blendMode = blendMode
this.canvasWidth = canvasWidth or 320
this.canvasHeight = canvasHeight or canvasWidth or 200
this.offsetX = 0
this.offsetY = 0
this.tilesCanvasW = math.floor(this.canvasWidth / this.size)
this.tilesCanvasH = math.floor(this.canvasHeight / this.size)
this.canvas = love.graphics.newCanvas(this.canvasWidth, this.canvasHeight)
return setmetatable(this, TileMap)
end
function TileMap:random(x, y)
local map = {}
local tilesCount = #self.tiles
-- Заполнение массива
for i = 1, y do
map[i] = {}
for j = 1, x do
map[i][j] = math.random(1, tilesCount)
end
end
self.map = map
end
function TileMap:update()
if self.map[1] == nil then
return
end
local mapHeight = #self.map
local mapWidth = #self.map[1]
local x = self.offsetX
local y = self.offsetY
local jOffset = math.max(math.floor(x / self.size) + 1, 1)
local iOffset = math.max(math.floor(y / self.size) + 1, 1)
-- print(x, iOffset)
if jOffset > mapWidth or iOffset > mapHeight or -x >= self.canvasWidth or -y >= self.canvasHeight then
return
end
local jUpper = math.min(jOffset + self.tilesCanvasW, mapWidth)
local iUpper = math.min(iOffset + self.tilesCanvasH, mapHeight)
local prevCanvas = love.graphics.getCanvas()
love.graphics.setCanvas(self.canvas)
local prevBlendMode = love.graphics.getBlendMode()
love.graphics.setBlendMode(self.blendMode)
love.graphics.clear(0, 255, 0) -- TODO: debug
for i = iOffset, iUpper do
for j = jOffset, jUpper do
local tileIndex = self.map[i][j]
love.graphics.draw(self.tiles[tileIndex], self.size * (j - 1) - x, self.size * (i - 1) - y)
-- love.graphics.print(tostring(tileIndex), self.size * (j - 1) - x, self.size * (i - 1) - y)
end
end
love.graphics.setBlendMode(prevBlendMode)
love.graphics.setCanvas(prevCanvas)
end
function TileMap:draw(x, y)
love.graphics.draw(self.canvas, x, y)
end