Enemy spawing and player tracking
I am new and Im trying to make enemies move towards the player and spawn(max 2-5), but how do I get that done?
I am new and Im trying to make enemies move towards the player and spawn(max 2-5), but how do I get that done?
What I do is I use code to figure out whether the player is to the left or right of the enemy and then I use that info to make it move towards the player. Essentially, I find if the player is to the enemy's left or right and than I move the enemy in the direction of the player.
Here is a basic example:
//In this code, X is right.
if enemy.x > player.x then
enemy.x -= 1
end
if enemy.x < player.x then
enemy.x += 1
end
And to spawn enemies, build their details as objects and then draw them in with screen.drawSprite() like any other sprite.
Actor = class
constructor = function(x, y, size, color, frameColor = "orange", liveCount = 100)
this.x = x
this.y = y
this.size = size
this.color = color
this.frameColor = frameColor
this.liveCount = liveCount
this.drawLiveCount = true
end
draw = function()
screen.fillRect(this.x, this.y, this.size, this.size, this.color)
screen.drawRect(this.x, this.y, this.size, this.size, this.frameColor)
if this.drawLiveCount then
screen.drawText(this.liveCount, this.x, this.y+this.size/2+this.size*0.2, 10, this.color)
end
end
end
Enemy = class extends Actor
constructor = function(x, y)
super(x, y, 20, "red")
end
hit = function( bullet )
this.liveCount -= bullet.power
end
update = function(target)
local direct_x = target.x - this.x
local direct_y = target.y - this.y
if direct_x < 0 then
this.x -= 0.1
else
this.x += 0.1
end
if direct_y < 0 then
this.y -= 0.1
else
this.y += 0.1
end
end
end
Player = class extends Actor
constructor = function(x, y)
super(x, y, 30, "green")
this.bullets = []
end
update = function()
if keyboard.UP then
this.y += 1
elsif keyboard.DOWN then
this.y -= 1
elsif keyboard.LEFT then
this.x -= 1
elsif keyboard.RIGHT then
this.x += 1
end
if keyboard.SPACE and this.bullets.length < 5 then
local direction = object x= 0 y= 0 end
if keyboard.UP then direction.y = 1
elsif keyboard.DOWN then direction.y = -1
elsif keyboard.LEFT then direction.x = -1
elsif keyboard.RIGHT then direction.x = 1
end
if direction.x != 0 or direction.y != 0 then
this.bullets.push(new Bullet(this.x, this.y, direction))
end
end
for bullet in this.bullets
bullet.update()
end
this.bullets = this.bullets.filter(function(bullet)
return bullet.x > -screen.width/2 and bullet.x < screen.width/2 and
bullet.y > -screen.height/2 and bullet.y < screen.height/2
end)
end
draw = function()
super()
for bullet in this.bullets
bullet.draw()
end
end
end
Bullet = class extends Actor
constructor = function(x, y, direction)
super(x, y, 5, "yellow")
this.direction = direction
this.speed = 5
this.drawLiveCount = false
this.power = 5
end
update = function()
this.x += this.direction.x * this.speed
this.y += this.direction.y * this.speed
end
end
init = function()
player = new Player(0, 0)
enemies = []
for index = 1 to 20 by 1
enemies.push(new Enemy(random.nextInt(screen.width) - screen.width / 2,
random.nextInt(screen.height) - screen.height / 2))
end
end
update = function()
player.update()
enemies.forEach(function(enemy) enemy.update(player) end)
for bullet in player.bullets
for enemy in enemies
if checkCollision(bullet, enemy) then
enemy.hit( bullet )
if enemy.liveCount < 1 then
enemies = enemies.filter(function(e) return e != enemy end)
end
player.bullets = player.bullets.filter(function(b) return b != bullet end)
end
end
end
end
draw = function()
screen.clear()
screen.drawText("WASD or Arrow to Move, SPACE to Shoot", 0, 90, 20, "orange")
player.draw()
enemies.forEach(function(enemy) enemy.draw() end)
end
checkCollision = function(obj1, obj2)
return abs(obj1.x - obj2.x) < (obj1.size + obj2.size) / 2 and
abs(obj1.y - obj2.y) < (obj1.size + obj2.size) / 2
end