-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.lua
More file actions
160 lines (125 loc) · 4.34 KB
/
Copy pathgame.lua
File metadata and controls
160 lines (125 loc) · 4.34 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Game = Object:extend();
local GRAVITY = 14.8;
function Game:new()
self.level = "maps/test_map.lua";
self.entities = {}
self.debug = {}
self.player = {};
self:loadAssets();
self:loadLevel();
end
function Game:loadAssets()
self.charImage = love.graphics.newImage('sprites/main_char.png')
self.crate = love.graphics.newImage("sprites/crate.png")
end
function Game:setupPhysics()
self.world = bump.newWorld()
self.world.gravity = GRAVITY
end
function Game:loadLevel()
self.entities = {}
self:setupPhysics();
self.map = sti(self.level, { "bump" })
self.map:bump_init(self.world)
for k, object in pairs(self.map.objects) do
if object.name == "player_spawn" then
self.player = Player(object.x, object.y, 28, 49, self.charImage, self.world, 200, 64, 100)
table.insert(self.entities, self.player)
elseif object.name == "box" then
local box = DynamicEntity(object.x, object.y, 32, 32, self.crate, self.world, 200, 64, "ent_crate", 10);
table.insert(self.entities, box)
elseif object.name == "spike" then
local spike = Entity(object.x, object.y, object.width, object.height, nil, self.world, "ent_spike")
table.insert(self.entities, spike)
elseif object.name == "level_end" then
local level_end = Entity(object.x, object.y, 35, 50, nil, self.world, "ent_level_end")
level_end.nextMap = object.properties.next_map;
table.insert(self.entities, level_end)
end
end
self.map:removeLayer("Objects")
end
function Game:checkCols(entity, cols)
local thisName = entity.name;
entity.grounded = false
for i,v in ipairs (cols) do
local otherName = cols[i].other.name;
if (thisName == "ent_player" or thisName == "ent_crate") and otherName == "ent_crate" and cols[i].normal.y ~= -1 then
cols[i].other.direction = entity.direction
cols[i].other.xVel = (entity.xVel);
--self.player.grippedEntity = cols[i].other
elseif thisName == "ent_player" and otherName == "ent_level_end" then
self.level = cols[i].other.nextMap
self:loadLevel();
elseif thisName == "ent_player" and otherName == "ent_spike" then
self:loadLevel();
end
if cols[i].normal.y == -1 then
entity.yVel = 0
entity.grounded = true
elseif cols[i].normal.y == 1 then
entity.yVel = -entity.yVel/4
end
if cols[i].normal.x ~= 0 and otherName == nil then
entity.xVel = 0
end
end
end
function Game:update(dt)
self.map:update(dt)
self:manageKeyboard(dt);
if self.player.grippedEntity ~= nil and self.player.isGripping then
self.player.grippedEntity.direction = self.player.direction
self.player.grippedEntity.maxVelX = self.player.maxVelX
self.player.grippedEntity.xVel = self.player.xVel
end
for i=1,#self.entities do
if self.entities[i]:is(DynamicEntity) then
self.entities[i]:updatePhysics(dt)
end
self.entities[i].x, self.entities[i].y, cols = self.world:move( self.entities[i], self.entities[i].x, self.entities[i].y )
self:checkCols(self.entities[i], cols)
end
self.player:update(dt)
end
function Game:manageKeyboard(dt)
if love.keyboard.isDown( "d" ) then
self.player:moveRight(dt)
elseif love.keyboard.isDown("a") then
self.player:moveLeft(dt);
end
if love.keyboard.isDown("w") then
self.player:jump(dt)
end
if love.keyboard.isDown("s") then
if self.player.isGripping == false then
for i=1,#self.entities do
if self.player:isEntityGrippable(self.entities[i]) then
self.player.isGripping = true;
self.player.grippedEntity = self.entities[i];
break
end
end
end
else
self.player.isGripping = false;
self.player.grippedEntity = nil
end
end
function Game:draw()
love.graphics.setColor( 255,255,255,255 )
--map:setDrawRange(0, 0, 800, 600)
self.map:draw()
love.graphics.setColor( 255, 93, 0,255 )
--love.graphics.rectangle( "fill",player.x,player.y,player.w,player.h )
love.graphics.setColor(255, 255, 0, 255)
--self.map:bump_draw(self.world)
--love.graphics.print(self.player.yVel..self.debug..tostring(self.player.grounded))
love.graphics.print(self.player.x,0,12)
love.graphics.print(self.player.y,0,24)
love.graphics.print(self.player.direction .. tostring(self.player.isGripping), 0, 36)
debug = " "
for i=1,#self.entities do
self.entities[i]:draw()
end
end