-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIsoExample.lua
125 lines (105 loc) · 4.45 KB
/
IsoExample.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
-- Setup
local loader = require("AdvTiledLoader.Loader")
loader.path = "maps/"
local map = loader.load("isometric.tmx")
local layer = map.layers.Ground
---------------------------------------------------------------------------------------------------
-- The guy we're going to be moving around
local Guy = {
tileX = 1, -- The horizontal tile
tileY = 1, -- The vertical tile
facing = "downleft", -- The direction our guy is facing
quads = { -- The frames of the image
down = love.graphics.newQuad(0,0,32,64,256,64),
downright = love.graphics.newQuad(32,0,32,64,256,64),
right = love.graphics.newQuad(64,0,32,64,256,64),
upright = love.graphics.newQuad(96,0,32,64,256,64),
up = love.graphics.newQuad(128,0,32,64,256,64),
upleft = love.graphics.newQuad(160,0,32,64,256,64),
left = love.graphics.newQuad(192,0,32,64,256,64),
downleft = love.graphics.newQuad(224,0,32,64,256,64),
},
-- The image
image = love.graphics.newImage("images/guy.png"),
}
---------------------------------------------------------------------------------------------------
-- Move the guy to the relative location
function Guy.move(x,y)
-- Change the facing direction
if x > 0 then Guy.facing = "downright"
elseif x < 0 then Guy.facing = "upleft"
elseif y > 0 then Guy.facing = "downleft"
else Guy.facing = "upright" end
-- Grab the tile
local tile = layer(Guy.tileX+x, Guy.tileY+y)
-- If the tile doesn't exist or is an obstacle then exit the function
if tile == nil then return end
if tile.properties.obstacle then return end
-- Otherwise change the guy's tile
Guy.tileX = Guy.tileX + x
Guy.tileY = Guy.tileY + y
end
---------------------------------------------------------------------------------------------------
-- Draw our guy. This function is passed to TileSet.drawAfterTile() which calls it passing the
-- x and y value of the bottom left corner of the tile.
function Guy.draw(x,y)
love.graphics.drawq(Guy.image, Guy.quads[Guy.facing], x+15, y-80)
end
---------------------------------------------------------------------------------------------------
-- Our example class
local IsoExample = {}
---------------------------------------------------------------------------------------------------
-- Called from love.keypressed()
function IsoExample.keypressed(k)
if k == 'w' then Guy.move(0,-1) end
if k == 'a' then Guy.move(-1,0) end
if k == 's' then Guy.move(0,1) end
if k == 'd' then Guy.move(1,0) end
end
---------------------------------------------------------------------------------------------------
-- Resets the example
function IsoExample.reset()
global.tx = -840
global.ty = 280
Guy.tileX = 1
Guy.tileY = 1
Guy.facing = "downleft"
displayTime = 0
end
---------------------------------------------------------------------------------------------------
-- Update the display time for the character control instructions
function IsoExample.update(dt)
displayTime = displayTime + dt
end
---------------------------------------------------------------------------------------------------
-- Add the guy to be drawn after the tile he's on
layer:setAfterTileFunction( function(layer, tileX, tileY, drawX, drawY)
if tileX == Guy.tileX and tileY == Guy.tileY then Guy.draw(drawX, drawY) end
end )
---------------------------------------------------------------------------------------------------
-- Called from love.draw()
function IsoExample.draw()
-- Set sprite batches
map.useSpriteBatch = global.useBatch
-- Scale and translate the game screen for map drawing
local ftx, fty = math.floor(global.tx), math.floor(global.ty)
love.graphics.push()
love.graphics.scale(global.scale)
love.graphics.translate(ftx, fty)
-- Limit the draw range
if global.limitDrawing then
map:autoDrawRange(ftx, fty, global.scale, -100)
else
map:autoDrawRange(ftx, fty, global.scale, 50)
end
-- Queue our guy to be drawn after the tile he's on and then draw the map.
local maxDraw = global.benchmark and 20 or 1
for i=1,maxDraw do
map:draw()
end
love.graphics.rectangle("line", map:getDrawRange())
-- Reset the scale and translation.
love.graphics.pop()
end
---------------------------------------------------------------------------------------------------
return IsoExample