im doing a Roguelike and i need help
i need help, ive done a enemy that follows my player, but i want to have multiple enemy at the same time, can someone help me how this works? https://microstudio.io/i/Keksmiau/neuesspiel/
i need help, ive done a enemy that follows my player, but i want to have multiple enemy at the same time, can someone help me how this works? https://microstudio.io/i/Keksmiau/neuesspiel/
You have to put your enemys in a list
enemys = []
enemy_count = 0
// function to make new enemys
makeEnemy = function(x, y)
enemys[enemy_count] = object end
enemys[enemy_count].x = x
enemys[enemy_count].y = y
enemys[enemy_count].speed = 1 // or whatever varible you want you enemys to have, can alos be way way more
enemy_count += 1
end
// then in your update loop
update = function()
for i=0 to enemy_count-1
if enemy_count > 0 then
// custom enemy logic, here is an example
enemys[i].x += 1 // enemy walks forward, put your own following ai here
// and also put enemy drawing code in here
end
end
end
With this code you can add enemy's with the makeEnemy function here is an example
init = function()
every 5 seconds do
makeEnemy(-100, 0)
end
end
// this code will add an enemy every 5 seconds
Hope this helps!!! :D
Can i also do a class
and how whould that look
yes you can do a class basically
enemy = class
constructor = function(speed,hp,x,y)
this.speed = speed
this.hp = hp
this.x = x
this.y = y
this.alive = 1
end
update = function()
if this.alive == 1 then
enemyAi(this.speed)
end
if this.hp <= 0 then
this.alive == 0
end
end
draw = function()
if alive == 1 then
screen.drawSprite("enemy",this.x,this.y,20,20)
end
end
end
when handling large amounts of things like enemys put it in a list enemies = [] to add a new enemy just do enemies.push(new enemy(speed,hp,x,y) make sure to replace the speed hp x and y with actual values and then in the update just make sure to
for i in enemies
i.update()
end
then copy that into draw and replace i.update() with i.draw()
a class is the best way