Re: Simple Tiled Implementation - STI v0.18.1.0
Posted: Wed Apr 12, 2017 11:45 pm
when you draw the player, floor their position too.
Code: Select all
local sti = require "sti"
local tx, ty, sx, sy, map
function love.load()
map = sti("assets/map.lua")
tx, ty = 0, 0
sx, sy = 1.5, 1.5
end
function love.update(dt)
local k = love.keyboard.isDown
if k("left") then tx = tx - 150 * dt end
if k("right") then tx = tx + 150 * dt end
if k("up") then ty = ty - 150 * dt end
if k("down") then ty = ty + 150 * dt end
end
function love.draw()
map:draw(-tx, -ty, sx, sy)
end
Code: Select all
require "object"
require "player"
require "msgbox"
local sti = require "Simple-Tiled-Implementation-master/sti"
function love.load()
game_map = 1
love.window.setTitle("Elkiria")
world_map01 = love.physics.newWorld(0, 0)
map_intro = sti("data/map_intro.lua", { "box2d" })
map_intro:box2d_init(world_map01)
spr_player = love.graphics.newImage("data/spr_adam.png")
fnt_system = love.graphics.newFont("data/arial.ttf", 20)
fnt_dialogue = love.graphics.newFont("data/FreePixel.ttf", 20)
end
function love.update(dt)
map_intro:update(dt)
Player:update(dt)
Msgbox:keypressed()
end
function love.draw()
map_intro:draw()
map_intro:box2d_draw()
Player:draw(spr_player, Player.xcoor, Player.ycoor)
love.graphics.setFont(fnt_system)
love.graphics.print(Player.xcoor, 0, 0)
love.graphics.print(Player.ycoor, 0, 20)
if msgbox_01 == true then
Msgbox:draw("Hello world!", 70, 440, 660, 110)
end
end
Code: Select all
require "core"
Object = {}
Object.__index = Object
Object.speed = 0
Object.state = "idle"
Object.xcoor = 0
Object.ycoor = 0
return Object
Code: Select all
require "core"
require "object"
Player = {}
Player.__index = Player
setmetatable(Object, Player)
Player.xcoor = 100
Player.ycoor = 100
Player.speed = 2
function Player:update(dt)
if love.keyboard.isDown("down") then
Player.state = "moving"
Player.ycoor = Player.ycoor + Player.speed
end
if love.keyboard.isDown("up") then
Player.state = "moving"
Player.ycoor = Player.ycoor - Player.speed
end
if love.keyboard.isDown("left") then
Player.state = "moving"
Player.xcoor = Player.xcoor - Player.speed
end
if love.keyboard.isDown("right") then
Player.state = "moving"
Player.xcoor = Player.xcoor + Player.speed
end
if not love.keyboard.isDown("down", "up", "left", "right") then
Player.state = "idle"
end
end
function Player:draw(sprite, xcoor, ycoor)
love.graphics.draw(sprite, xcoor, ycoor)
end
return Player
Code: Select all
require "core"
Msgbox = {}
Msgbox.__index = Msgbox
msgbox_01 = false
function Msgbox:keypressed()
function love.keypressed(key)
if key == "z" then
msgbox_01 = true
elseif key == "x" then
msgbox_01 = false
end
end
end
function Msgbox:draw(string, x, y, width, height)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", x, y, width, height)
love.graphics.setColor(255, 255, 255)
love.graphics.print(string, x + 10, y + 10)
end
Code: Select all
local function setFlippedGID(gid, object)
if gid ~= nil then
local bit31 = 2147483648
local bit30 = 1073741824
local bit29 = 536870912
local flipX = false
local flipY = false
local flipD = false
local sx = false
local sy = false
local realgid = gid
if realgid >= bit31 then
realgid = realgid - bit31
flipX = not flipX
end
if realgid >= bit30 then
realgid = realgid - bit30
flipY = not flipY
end
if realgid >= bit29 then
realgid = realgid - bit29
flipD = not flipD
end
if flipX then
if flipY and flipD then
sx = true
sy = true
elseif flipY then
sx = true
sy = true
elseif flipD then
sx = true
else
sx = true
end
elseif flipY then
sy = true
end
if sx then object.x = object.x - object.w end
if sy then object.y = object.y - object.h end
end
return object
end