-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDesertExample.lua
127 lines (106 loc) · 4.41 KB
/
DesertExample.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
126
127
-- Setup
local loader = require("AdvTiledLoader.Loader")
loader.path = "maps/"
local map = loader.load("desert.tmx")
local tileLayer = map.layers["Ground"]
---------------------------------------------------------------------------------------------------
-- This is the guy we'll be moving around.
local Guy = {}
Guy.x = 0
Guy.y = 0
Guy.tileX = 8 -- The horizontal tile
Guy.tileY = 22 -- The vertical tile
Guy.facing = "down" -- The direction our guy is facing
Guy.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 of our guy
Guy.image = love.graphics.newImage("images/guy.png")
Guy.width = Guy.image:getWidth()
Guy.height = Guy.image:getHeight()
---------------------------------------------------------------------------------------------------
-- Move the guy around the tiles
function Guy.moveTile(x,y)
-- Change the facing direction
if x > 0 then Guy.facing = "right"
elseif x < 0 then Guy.facing = "left"
elseif y > 0 then Guy.facing = "down"
else Guy.facing = "up" end
-- Grab the tile
local tile = tileLayer(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 location
Guy.tileX = Guy.tileX + x
Guy.tileY = Guy.tileY + y
Guy.x = Guy.tileX * map.tileWidth
Guy.y = (Guy.tileY + 1) * map.tileHeight - Guy.height
end
---------------------------------------------------------------------------------------------------
-- Do this at first to make sure the guy is drawn correctly.
Guy.moveTile(0,0)
Guy.facing = "down"
---------------------------------------------------------------------------------------------------
-- Our example class
local DesertExample = {}
---------------------------------------------------------------------------------------------------
-- Called from love.keypressed()
function DesertExample.keypressed(k)
if k == 'w' then Guy.moveTile(0,-1) end
if k == 'a' then Guy.moveTile(-1,0) end
if k == 's' then Guy.moveTile(0,1) end
if k == 'd' then Guy.moveTile(1,0) end
end
---------------------------------------------------------------------------------------------------
-- Resets the example
function DesertExample.reset()
global.tx = -5
global.ty = -434
Guy.tileX = 8
Guy.tileY = 22
Guy.moveTile(0,0)
Guy.facing = "down"
displayTime = 0
end
---------------------------------------------------------------------------------------------------
-- Update the display time for the character control instructions
function DesertExample.update(dt)
displayTime = displayTime + dt
end
---------------------------------------------------------------------------------------------------
-- Called from love.draw()
function DesertExample.draw()
-- Set sprite batches if they are different than the settings.
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()
love.graphics.drawq(Guy.image, Guy.quads[Guy.facing], Guy.x, Guy.y)
end
love.graphics.rectangle("line", map:getDrawRange())
-- Reset the scale and translation.
love.graphics.pop()
end
---------------------------------------------------------------------------------------------------
return DesertExample