Skip to content

Commit

Permalink
Enemies now change direction when they hit an object
Browse files Browse the repository at this point in the history
  • Loading branch information
Nullh committed Oct 2, 2016
1 parent ba7ead4 commit 9c1b484
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 27 deletions.
18 changes: 13 additions & 5 deletions baddie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function baddie:initialize(x, y, speed, collider, g, builder)
self._speed = speed
self._g = g
self._dead = false
self._facingRight = false
self._collObj = collider:rectangle(self._x, self._y, self._spriteWidth, self._spriteHeight)
self._grid = anim8.newGrid(self._spriteWidth, self._spriteHeight, self._sprite:getWidth(), self._sprite:getHeight())
self._animations = {}
Expand All @@ -30,8 +31,12 @@ end
function baddie:update(dt, fallLimit)

self._moving = true
self._facingRight = false
self._x = self._x - (self._speed * dt)

if self._facingRight then
self._x = self._x + (self._speed * dt)
else
self._x = self._x - (self._speed * dt)
end

self._yVelocity = self._yVelocity + (self._g * dt)
self._y = self._y + self._yVelocity
Expand All @@ -43,9 +48,12 @@ function baddie:update(dt, fallLimit)
if delta.y < 0 then
self._yVelocity = 0
end
if delta.x < 0 then
self._xVelocity = 0
self._moving = false
if delta.x ~= 0 then
if self._facingRight then
self._facingRight = false
else
self._facingRight = true
end
end
end
end
Expand Down
99 changes: 82 additions & 17 deletions maps/map4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ return {
height = 20,
tilewidth = 8,
tileheight = 8,
nextobjectid = 13,
nextobjectid = 20,
properties = {},
tilesets = {
{
Expand Down Expand Up @@ -135,9 +135,9 @@ return {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
}
},
Expand Down Expand Up @@ -241,6 +241,71 @@ return {
rotation = 0,
visible = true,
properties = {}
},
{
id = 14,
name = "",
type = "",
shape = "rectangle",
x = 95.875,
y = 144.25,
width = 8.25,
height = 10.375,
rotation = 0,
visible = true,
properties = {}
},
{
id = 15,
name = "",
type = "",
shape = "rectangle",
x = 48.125,
y = 128,
width = 8,
height = 29.25,
rotation = 0,
visible = true,
properties = {}
},
{
id = 16,
name = "",
type = "",
shape = "rectangle",
x = 40.125,
y = 136.25,
width = 8.875,
height = 21.375,
rotation = 0,
visible = true,
properties = {}
},
{
id = 17,
name = "",
type = "",
shape = "rectangle",
x = 31.875,
y = 144.25,
width = 10,
height = 12.75,
rotation = 0,
visible = true,
properties = {}
},
{
id = 19,
name = "",
type = "",
shape = "rectangle",
x = 288.125,
y = 136,
width = 7.875,
height = 22.75,
rotation = 0,
visible = true,
properties = {}
}
}
},
Expand All @@ -254,19 +319,6 @@ return {
draworder = "topdown",
properties = {},
objects = {
{
id = 8,
name = "",
type = "",
shape = "rectangle",
x = 167.082,
y = 111.183,
width = 5.29755,
height = 3.88487,
rotation = 0,
visible = true,
properties = {}
},
{
id = 10,
name = "",
Expand All @@ -292,6 +344,19 @@ return {
rotation = 0,
visible = true,
properties = {}
},
{
id = 18,
name = "",
type = "",
shape = "rectangle",
x = 74.5,
y = 144.875,
width = 3.125,
height = 2.875,
rotation = 0,
visible = true,
properties = {}
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions maps/map4.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="50" height="20" tilewidth="8" tileheight="8" nextobjectid="13">
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="50" height="20" tilewidth="8" tileheight="8" nextobjectid="20">
<tileset firstgid="1" name="Blocks 8x8" tilewidth="8" tileheight="8" tilecount="4" columns="2">
<image source="../assets/Blocks 8x8.png" trans="000000" width="16" height="16"/>
</tileset>
Expand Down Expand Up @@ -69,9 +69,9 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
</data>
</layer>
Expand All @@ -83,10 +83,15 @@
<object id="5" x="176.007" y="48.0426" width="47.864" height="7.76897"/>
<object id="6" x="143.949" y="23.932" width="48.1319" height="7.85827"/>
<object id="7" x="200.207" y="7.85827" width="31.7903" height="8.30476"/>
<object id="14" x="95.875" y="144.25" width="8.25" height="10.375"/>
<object id="15" x="48.125" y="128" width="8" height="29.25"/>
<object id="16" x="40.125" y="136.25" width="8.875" height="21.375"/>
<object id="17" x="31.875" y="144.25" width="10" height="12.75"/>
<object id="19" x="288.125" y="136" width="7.875" height="22.75"/>
</objectgroup>
<objectgroup name="enemies">
<object id="8" x="167.082" y="111.183" width="5.29755" height="3.88487"/>
<object id="10" x="155.748" y="66.3959" width="3.17853" height="3.17853"/>
<object id="12" x="117.695" y="146.449" width="3.03615" height="2.76825"/>
<object id="18" x="74.5" y="144.875" width="3.125" height="2.875"/>
</objectgroup>
</map>

0 comments on commit 9c1b484

Please sign in to comment.