diff --git a/ai/ai.lua b/ai/ai.lua index aeec33c..0dcbb1d 100644 --- a/ai/ai.lua +++ b/ai/ai.lua @@ -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 diff --git a/enemy.lua b/enemy.lua index 5fee044..4f12fc9 100644 --- a/enemy.lua +++ b/enemy.lua @@ -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 @@ -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) @@ -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!) diff --git a/game.lua b/game.lua index 7867734..ddcbf7f 100644 --- a/game.lua +++ b/game.lua @@ -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 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 diff --git a/tower.lua b/tower.lua index 5df3c8d..90dc67b 100644 --- a/tower.lua +++ b/tower.lua @@ -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 \ No newline at end of file diff --git a/towerType.lua b/towerType.lua index bd5c320..381be6f 100644 --- a/towerType.lua +++ b/towerType.lua @@ -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