Skip to content

Commit

Permalink
Reworked code to reduce for loops and optimize object logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
kobaltoxid committed Jan 20, 2021
1 parent dc2f02d commit dc94f9e
Show file tree
Hide file tree
Showing 10 changed files with 3,734 additions and 610 deletions.
3,098 changes: 3,098 additions & 0 deletions assets/dungeon/final_version.lua

Large diffs are not rendered by default.

752 changes: 383 additions & 369 deletions assets/dungeon/merged.tmx

Large diffs are not rendered by default.

38 changes: 26 additions & 12 deletions enemy.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- Default enemy object.
local Enemy = {
name = 'enemy',
sprite = '',
xPos = 0,
yPos = 0,
originX = 0,
Expand All @@ -8,6 +10,8 @@ local Enemy = {
height = 16,
damage = 1,
health = 100,
dir = 1,
flip = 1,
alive = true
}

Expand All @@ -20,8 +24,8 @@ function Enemy:new(o)
end

-- Self explanatory.
function Enemy:attack(po)
local result = po.health - self.damage
function Enemy:attack(p)
local result = p.health - self.damage
return result
end

Expand All @@ -32,7 +36,7 @@ local axisY = false
local direction = 0

-- Default enemy movement.
function Enemy:walk(goalX, goalY, dir, dt)
function Enemy:walk(goalX, goalY, dt)
local stoptimer = 5 -- 5 seconds
local speed = 50

Expand All @@ -41,21 +45,21 @@ function Enemy:walk(goalX, goalY, dir, dt)
direction = math.random(1, 1000323)
initialtime = love.timer.getTime()
if direction % 3 == 0 then
dir = dir * -1
self.dir = self.dir * -1
else
dir = dir * 1
self.dir = self.dir * 1
end
-- dir = smartTimes % 3 == 0 and dir * -1 or dir * 1
axisY = axisY == false and true or false
end

if axisY == true then
goalY = goalY + speed * dt * dir
goalY = goalY + speed * dt * self.dir
else
goalX = goalX + speed * dt * dir
goalX = goalX + speed * dt * self.dir
end

return goalX, goalY, dir
return goalX, goalY, self.dir

end

Expand All @@ -68,14 +72,13 @@ function Enemy:setPos(xPos, yPos)
end

-- Flip sprite.
function Enemy:flip(direction)
self.dir = direction
function Enemy:flipEnemy(flip)
self.flip = flip
end

-- Set enemy sprite.
function Enemy:setSprite(quad)
self.sprite = quad
-- self.sprite = love.graphics.newImage(path)
end

-- Calculate distance between two points.
Expand All @@ -84,8 +87,9 @@ function dist(x1, y1, x2, y2)
end

-- Function to return what coordinates to chase.
function Enemy:chase(actualX, actualY, playerX, playerY, dt)
function Enemy:chase(playerX, playerY, dt)
local speed = 80
local actualX, actualY = self.xPos, self.yPos
local goalX = playerX - actualX
local goalY = playerY - actualY
local distance = math.sqrt(goalX * goalX + goalY * goalY)
Expand All @@ -101,4 +105,14 @@ function Enemy:changeDamage(damage)
self.damage = damage
end

-- Change enemy object direction.
function Enemy:changeDir(dir)
self.dir = dir
end

-- Change enemy type.
function Enemy:changeType(type)
self.name = type
end

return Enemy
6 changes: 6 additions & 0 deletions health_potion.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
health_potion = {
name = 'health_potion',
sprite = '',
x = 0,
y = 0,
hp = 50,
Expand All @@ -17,6 +19,10 @@ function health_potion:setPos(xPos, yPos)
self.x, self.y = xPos, yPos
end

function health_potion:setSprite(quad)
self.sprite = quad
end

function health_potion:take(po)
self.taken = true
po.health = po.health + self.hp
Expand Down
Binary file modified logonator.lovec
Binary file not shown.
8 changes: 6 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ end

function love.draw()
if map:getAliveStatus() == true and map:getWinStatus() == false then
love.graphics.print('Lives:', love.graphics.getWidth() - 580, love.graphics.getHeight() - 50)
love.graphics.print(map:getLives(), love.graphics.getWidth() - 540, love.graphics.getHeight() - 50)
love.graphics.print('Keys:', love.graphics.getWidth() - 520, love.graphics.getHeight() - 50)
love.graphics.print(map:getKeys(), love.graphics.getWidth() - 480, love.graphics.getHeight() - 50)
love.graphics.print('AD: ~30 DPS', love.graphics.getWidth() - 450, love.graphics.getHeight() - 50)
love.graphics.print('Health:', love.graphics.getWidth() - 350, love.graphics.getHeight() - 50)
love.graphics.print(map:drawHealth(), love.graphics.getWidth() - 280, love.graphics.getHeight() - 50)
Expand All @@ -40,8 +44,8 @@ function love.draw()

-- Translate world so that player is always centred
local player = map.layers["Sprites"].sprites.player
local tx = math.floor(player.x - (screen_width - 120) / 2)
local ty = math.floor(player.y - (screen_height - 50) / 2)
local tx = math.floor(player.xPos - (screen_width - 120) / 2)
local ty = math.floor(player.yPos - (screen_height - 50) / 2)

-- As basic as fog of war can get. Thank god I wasted 10 hours on more unoptimal solutions.
map:resize(screen_width - 120, screen_height - 50)
Expand Down
Loading

0 comments on commit dc94f9e

Please sign in to comment.