Skip to content

Commit

Permalink
Towers obey range according to type (which is the same for all types …
Browse files Browse the repository at this point in the history
…for now). This fixes issue #24.

Also made some minor changes so that the dead enemy and shots on it are no longer shown at game over.
  • Loading branch information
nczempin committed Apr 1, 2014
1 parent 9936539 commit 6f4d3ec
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
2 changes: 1 addition & 1 deletion ai/ai.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function aStar(start, goal, all_nodes)
local MAX_DIST = 1

if
distance ( node.x, node.y, neighbor.x, neighbor.y ) <= MAX_DIST then
distance_manhattan ( node.x, node.y, neighbor.x, neighbor.y ) <= MAX_DIST then
return true
end
return false
Expand Down
12 changes: 9 additions & 3 deletions enemy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function love.turris.newEnemy(img, map, x,y,baseX, baseY)
o.img = img
o.x = x
o.y = y
o.dead = false
o.waypoints = o.generateWaypoints(map,x,y,baseX,baseY)

o.currentWaypoint = 2
Expand Down Expand Up @@ -76,11 +77,15 @@ function love.turris.newEnemy(img, map, x,y,baseX, baseY)
return o
end

function distance(x1,y1,x2,y2) --TODO let's not leave this global
function distance_manhattan(x1,y1,x2,y2) --TODO let's not leave this global
-- manhattan is sufficient for now
return math.abs(x1-x2)+math.abs(y1-y2)
end

function distance_euclid(x1,y1,x2,y2)
local x = x1-x2
local y = y1-y2
return math.sqrt(x*x+y*y)
end
function love.turris.normalize(x,y)
local m = math.max(math.abs(x),math.abs(y))
--print ("normalize: ", x, y, m)
Expand Down Expand Up @@ -118,7 +123,8 @@ function love.turris.updateEnemies(o,dt)
-- write back the changes
o.enemies[i]= e
-- check for and handle game over
if distance(o.baseX, o.baseY,x,y) < 1 then
if distance_manhattan(o.baseX, o.baseY,x,y) < 1 then
e.dead = true
-- Game Over!!! (for now)
-- TODO: destroy ship (explosion)
-- TODO: destroy base (explosion!)
Expand Down
97 changes: 52 additions & 45 deletions game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function love.turris.newGame()
local state = o.map.getState(x,y)
if state and state ==0 then
--print ("x, y:",x,y)
local t = love.turris.newTower(type, x, y)
local t = love.turris.newTower(o.towerType[type], x, y)
--print ("tower: ",t, t.x, t.y)
o.map.setState(t.x, t.y, type)
if o.towers.next<o.towers.maxamount and not o.towers.next then
Expand Down Expand Up @@ -108,7 +108,7 @@ function love.turris.newGame()
local dirX,dirY = love.turris.normalize(deltaX , deltaY)

if dirX ~= dirX or dirY ~= dirY then
--print ("NaN")
--print ("NaN")
else
dirX = math.floor(dirX)
dirY= math.floor(dirY)
Expand Down Expand Up @@ -274,6 +274,7 @@ function love.turris.newGame()
lightWorld.drawGlow()
end
end

o.draw = function()
o.drawMap()
o.drawShots()
Expand All @@ -282,21 +283,24 @@ function love.turris.newGame()
end

o.drawShots = function()
local e = o.enemies[1] -- TODO this is a hack because I know there's only one creep for now

local x, y = e.x, e.y
G.setColor(255, 0, 0)

for i = 1, o.towerCount do
-- TODO which tower shoots what should be determined in update(); here we should only draw what has already been determined
local t = o.towers[i]
local tx = (t.x - 0.5) * o.map.tileWidth + o.offsetX
local ty = (t.y - 0.5) * o.map.tileHeight + o.offsetY
local ex = (e.x - 0.5) * o.map.tileWidth + o.offsetX
local ey = (e.y - 0.5) * o.map.tileHeight + o.offsetY
local direction = math.atan2(tx - ex, ey - ty) + math.pi * 0.5
local length = math.sqrt(math.pow(tx - ex, 2) + math.pow(ty - ey, 2))
if (length < 150)then
local e = t.determineTarget(o.enemies,distance_euclid)

if e then
local x, y = e.x, e.y

local tx = (t.x - 0.5) * o.map.tileWidth + o.offsetX
local ty = (t.y - 0.5) * o.map.tileHeight + o.offsetY
local ex = (e.x - 0.5) * o.map.tileWidth + o.offsetX
local ey = (e.y - 0.5) * o.map.tileHeight + o.offsetY
local direction = math.atan2(tx - ex, ey - ty) + math.pi * 0.5
local length = math.sqrt(math.pow(tx - ex, 2) + math.pow(ty - ey, 2))
-- if (length < 150)then
local timer = -math.mod(love.timer.getTime() * 2.0, 1.0)
G.setBlendMode("additive")
--local vertices = {
Expand All @@ -313,6 +317,7 @@ function love.turris.newGame()
}
o.mshLaser:setVertices(vertices)
G.draw(o.mshLaser, (t.x - 0.5) * o.map.tileWidth + o.offsetX, (t.y - 0.5) * o.map.tileHeight + o.offsetY, direction, length / o.imgLaser:getWidth(), 1, 0, 64)
-- end
end
end
end
Expand Down Expand Up @@ -346,42 +351,44 @@ function love.turris.newGame()
o.drawEnemies = function()
for i = 1, o.enemyCount do
local e = o.enemies[i]
local x = e.x
local y = e.y
local img = e.img
G.setColor(15, 15, 31, 63 + math.sin(love.timer.getTime() * 2.0) * 31)
love.graphics.ellipse("fill", x * o.map.tileWidth - o.offsetX - 32, y * o.map.tileHeight + o.offsetY - 16, 12 + math.sin(love.timer.getTime() * 2.0) * 3, 8 + math.sin(love.timer.getTime() * 2.0) * 2, 0, 16)
G.setColor(255, 255, 255)
local directionAnim = (e.getDirection() + math.pi) / (math.pi * 0.25) - 1
if directionAnim == 0 then
directionAnim = 8
end
if e and not e.dead then
local x = e.x
local y = e.y
local img = e.img
G.setColor(15, 15, 31, 63 + math.sin(love.timer.getTime() * 2.0) * 31)
love.graphics.ellipse("fill", x * o.map.tileWidth - o.offsetX - 32, y * o.map.tileHeight + o.offsetY - 16, 12 + math.sin(love.timer.getTime() * 2.0) * 3, 8 + math.sin(love.timer.getTime() * 2.0) * 2, 0, 16)
G.setColor(255, 255, 255)
local directionAnim = (e.getDirection() + math.pi) / (math.pi * 0.25) - 1
if directionAnim == 0 then
directionAnim = 8
end

o.creepAnim:seek(directionAnim)

o.creepAnim:draw(x * o.map.tileWidth - (o.creepImg:getWidth() * 0.5) + o.offsetX - 32, (y - 1) * o.map.tileHeight - (o.creepImg:getHeight() / 8.0 * 0.5) + o.offsetY + 16 + math.sin(love.timer.getTime() * 2.0) * 4.0)
--e.shadow.setPosition(x * o.map.tileWidth - (o.creepImg:getWidth() * 0.5) + o.offsetX - 32, (y - 1) * o.map.tileHeight - (o.creepImg:getHeight() / 8.0 * 0.5) + o.offsetY + 32)
-- health
if e.health < e.maxHealth then
G.setColor(0, 0, 0, 127)
G.rectangle("fill", (x - 1) * o.map.tileWidth + o.offsetX - 2, (y - 1) * o.map.tileHeight + o.offsetY - 16 - 2, 64 + 4, 8 + 4)
G.setColor(255 * math.min((1.0 - e.health / e.maxHealth) * 2.0, 1.0), 255 * math.min((e.health / e.maxHealth) * 1.5, 1.0), 0)
G.rectangle("fill", (x - 1) * o.map.tileWidth + o.offsetX + 2, (y - 1) * o.map.tileHeight + o.offsetY - 16 + 2, (64 - 4) * e.health / e.maxHealth, 8 - 4)
G.setLineWidth(1)
G.setColor(255, 63, 0)
G.rectangle("line", (x - 1) * o.map.tileWidth + o.offsetX, (y - 1) * o.map.tileHeight + o.offsetY - 16, 64, 8)
end
-- test
if e.health > 0.0 then
e.health = e.health - 0.1
end
--print(e.getDirection())
o.creepAnim:seek(directionAnim)

o.creepAnim:draw(x * o.map.tileWidth - (o.creepImg:getWidth() * 0.5) + o.offsetX - 32, (y - 1) * o.map.tileHeight - (o.creepImg:getHeight() / 8.0 * 0.5) + o.offsetY + 16 + math.sin(love.timer.getTime() * 2.0) * 4.0)
--e.shadow.setPosition(x * o.map.tileWidth - (o.creepImg:getWidth() * 0.5) + o.offsetX - 32, (y - 1) * o.map.tileHeight - (o.creepImg:getHeight() / 8.0 * 0.5) + o.offsetY + 32)
-- health
if e.health < e.maxHealth then
G.setColor(0, 0, 0, 127)
G.rectangle("fill", (x - 1) * o.map.tileWidth + o.offsetX - 2, (y - 1) * o.map.tileHeight + o.offsetY - 16 - 2, 64 + 4, 8 + 4)
G.setColor(255 * math.min((1.0 - e.health / e.maxHealth) * 2.0, 1.0), 255 * math.min((e.health / e.maxHealth) * 1.5, 1.0), 0)
G.rectangle("fill", (x - 1) * o.map.tileWidth + o.offsetX + 2, (y - 1) * o.map.tileHeight + o.offsetY - 16 + 2, (64 - 4) * e.health / e.maxHealth, 8 - 4)
G.setLineWidth(1)
G.setColor(255, 63, 0)
G.rectangle("line", (x - 1) * o.map.tileWidth + o.offsetX, (y - 1) * o.map.tileHeight + o.offsetY - 16, 64, 8)
end
-- test
if e.health > 0.0 then
e.health = e.health - 0.1
end
--print(e.getDirection())

--debug: show travel direction
local ox, oy = e.getOrientation()
local wp = e.waypoints[e.currentWaypoint]
G.setColor(255, 63, 123)
o.drawLine(x,y,wp[1],wp[2])
--debug: show travel direction
local ox, oy = e.getOrientation()
local wp = e.waypoints[e.currentWaypoint]
G.setColor(255, 63, 123)
o.drawLine(x,y,wp[1],wp[2])
end
end
end

Expand Down
20 changes: 20 additions & 0 deletions tower.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
require "towerType"

function love.turris.newTower(type, x, y)
print ("new tower:", type, x, y)
local o = {}
o.x = x
o.y = y
o.type = type
o.shooting = false
o.target = {}

o.getRange = function()
return type.range
end

o.determineTarget = function(enemies,distance_function)
for i = 1, #enemies do
local e = enemies[i]
if not e.dead then
local d = distance_function(o.x,o.y, e.x,e.y)
if (d<=o.type.range)then
return e --TODO: for now we just pick the one existing enemy. Once we have more than one, we need to choose between the ones in range
end
end
end
end

return o
end
2 changes: 2 additions & 0 deletions towerType.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ function love.turris.newTowerType(img)
o.glow = G.newImage(img .. "_glow.png")
o.maxHealth = 100.0

o.range = 2

o.getMaxHealth = function(health)
return o.maxHealth
end
Expand Down

0 comments on commit 6f4d3ec

Please sign in to comment.