Issues with enemy behaviour
Posted: Sun Sep 23, 2018 5:19 pm
So i'm making a game with enemies that are supposed to follow you, and it doesn't work at all.
Her is the enemy file
here is the main file
Her is the enemy file
Code: Select all
local enemy = {}
function enemy:new()
local nenemy = {}
nenemy.x = love.math.random(1, Map.wandh[2])--player.x+2--Map.wandh[2]--
nenemy.y = love.math.random(1, Map.wandh[1]) --player.y+2--Map.wandh[1]--
nenemy.h = Map.heightmap[nenemy.y][nenemy.x]
nenemy.spr = love.graphics.newImage("assets/enemy.png")
nenemy.active = false
nenemy.str = 5
nenemy.range = 3
nenemy.id = "enemy"
nenemy.move = 5
nenemy.life = 30
nenemy.maxlife = 30
nenemy.spd = 0.1
nenemy.clickedon = false
nenemy.rx = 0
nenemy.ry = 0
nenemy.validMove = {}
nenemy.validrange = {}
table.insert(enemies, nenemy)
end
function enemy:set()
end
function enemy:update()
for i = #enemies, 1, -1 do
enemies[i].spd = (Map.height_val - enemies[i].h )/Map.height_val
if turn % 2 ~= 1 then
print("enemy turn :"..turn)
local dx = ((player.x + 2) - enemies[i].x)*enemies[i].spd
local dy = ((player.y + 2) - enemies[i].y)*enemies[i].spd
if enemies[i].life < enemies[i].maxlife/2.4 then
-- runs away from player
enemies[i].x = enemies[i].x - math.floor(dx)
enemies[i].y = enemies[i].y - math.floor(dy)
else
--chases player
enemies[i].x = enemies[i].x + math.floor(dx)
enemies[i].y = enemies[i].y + math.floor(dy)
saybug("The enemy's speed is :"..enemies[i].spd)
end
if enemies[i].y > Map.wandh[1] and
enemies[i].x > Map.wandh[2]
or enemies[i].y < 1
and enemies[i].x < 1 then
--temporary solution for off boudaries limit
enemies[i].h = 1
saybug("The enemy crossed boundaries")
else
enemies[i].h = Map.heightmap[enemies[i].y][enemies[i].x]
end
local vraipx = player.x+2
local vraipy = player.y+2 -- the true player position
local trange = enemies[i].range+1 -- the true range
--used to attack the player
if player.x+2 == enemies[i].x and player.y+2 == enemies[i].y then
player.hp = player.hp - enemies[i].str
saybug("The enemy attacked the player at:"..player.x+2 .. ";" .. player.y+2)
end
saybug("The enemy position is: "..enemies[i].x ..";".. enemies[i].y)
love.timer.sleep(0.5)
turn = turn + 1
end
if enemies[i].life < 1 then
saybug((enemies[i].id).." #"..(i).." is dead.")
enemies[i] = nil
end
end
end
function enemy:draw()
for i = #enemies, 1, -1 do
enemies[i].rx = ((enemies[i].x - 1) - (enemies[i].y - 1))* tile_size[1]/2
enemies[i].ry = (enemies[i].x + enemies[i].y) * tile_size[2]/2
--sprite
g.draw(enemies[i].spr, enemies[i].rx + camera.x, (enemies[i].ry - enemies[i].h * tile_size[2]/2) + camera.y)
--DEBUG
-------
g.rectangle("line", enemies[i].rx + camera.x, (enemies[i].ry - enemies[i].h * tile_size[2]/2) + camera.y, 27, 37)
--debug lifebar
g.setColor(0.87, 0.1, 0.23)
g.rectangle("fill", (enemies[i].rx + camera.x), (enemies[i].ry - enemies[i].h * tile_size[2]/2)-15 + camera.y, 75*enemies[i].life/enemies[i].maxlife, 10)
g.setColor(1, 1, 1)
--id
g.print(enemies[i].id.." ".."#"..i, (enemies[i].rx + camera.x), (enemies[i].ry - enemies[i].h * tile_size[2]/2)-20 + camera.y, 0, 1.1)
------
end
end
return enemy
Code: Select all
Map = require("map")
w, h, f = love.window.getMode()
camera = require("camera")
player = require("player")
mouse = require("mouse")
enemy = require("enemy") --until fucntion works
enemies = {}
turn = 1
bugs = {}
function saybug(txt)
table.insert(bugs, txt)
end
function love.load()
background = love.graphics.newImage("assets/background.png")
g = love.graphics
-- pixel art
love.graphics.setDefaultFilter("nearest")
love.graphics.setLineStyle("rough")
enemy:new()
enemy:new()
w, h, f = love.window.getMode()
end
function love.update(dt)
tile_size = {32/camera.scale, 16/camera.scale}
-- TODO create a table that stocks all camera affected objects
camera:update() --tile size is changed by camera
player:update()
mouse:update(Map)
enemy:update()
if love.keyboard.isDown("escape") then
love.window.setFullscreen(true)
w, h, f = love.window.getMode()
end
enemy:update()
if love.keyboard.isDown("a") then
love.window.setFullscreen(false)
w, h, f = love.window.getMode()
end
end
function love.draw()
love.graphics.setColor(0.8, 0.7, 1)
-- love.graphics.setColor(love.math.random(0.5, 1), love.math.random(0.5, 1), love.math.random(0.5, 1)) -- mode foo
love.graphics.rectangle("fill", 0, 0, w, h)
-- love.graphics.draw(background, 0, 0, 0, )
love.graphics.setColor(1, 1, 1)
Map:draw(camera, tile_size)
mouse.draw()
player:draw()
enemy:draw() -- until function works
--debug
pad = 250
for i=#bugs, 1, -1 do
g.print(bugs[i], w - pad, (i*10)-#bugs*2)
print(bugs[i])
end
end