Browse Source

enemies follow player

main
Stephanie Gredell 2 years ago
parent
commit
26667292f9
  1. 27
      Chat.lua
  2. 10
      Game.lua
  3. 2
      Player.lua
  4. BIN
      assets/f1065sk1.pdf
  5. 2
      main.lua

27
Chat.lua

@ -4,8 +4,9 @@ local lg = love.graphics
function Chat(x, y, level) function Chat(x, y, level)
local CHAT_SPEED = math.random(50) + (level * 2) local CHAT_SPEED = math.random(30) + (level * 2)
local vel = -1 local vel = -1
local life = 5
if math.random() < 0.5 then if math.random() < 0.5 then
vel = 1 vel = 1
@ -18,27 +19,27 @@ function Chat(x, y, level)
x_vel = math.random() * CHAT_SPEED * vel, x_vel = math.random() * CHAT_SPEED * vel,
y_vel = math.random() * CHAT_SPEED * vel, y_vel = math.random() * CHAT_SPEED * vel,
radius = 30, radius = 30,
life = 5,
draw = function(self) draw = function(self)
lg.draw(self.sprite, self.x, self.y, 0, 1, 1, self.sprite:getWidth() / 2, self.sprite:getHeight() / 2) lg.draw(self.sprite, self.x, self.y, 0, 1, 1, self.sprite:getWidth() / 2, self.sprite:getHeight() / 2)
lg.circle("line", self.x, self.y, self.radius) lg.circle("line", self.x, self.y, self.radius)
end, end,
move = function(self, dt) move = function(self, player_x, player_y, dt)
self.x = self.x + self.x_vel * dt -- direction is the vector from the enemy to the player
self.y = self.y + self.y_vel * dt local direction_x = player_x - self.x
local direction_y = player_y - self.y
local distance = math.sqrt(direction_x * direction_x + direction_y * direction_y)
if self.x + self.radius < 0 then if dist ~= 0 then -- avoid division by zero
self.x = love.graphics.getWidth() + self.radius self.x = self.x + direction_x / distance * CHAT_SPEED * dt
elseif self.x - self.radius > love.graphics.getWidth() then self.y = self.y + direction_y / distance * CHAT_SPEED * dt
self.x = -self.radius
end end
end,
if self.y + self.radius < 0 then decreaseLife = function(self)
self.y = love.graphics.getHeight() + self.radius self.life = self.life - 1
elseif self.y - self.radius > love.graphics.getHeight() then
self.y = -self.radius
end
end, end,
destroy = function(self, chat_table, index) destroy = function(self, chat_table, index)

10
Game.lua

@ -24,13 +24,11 @@ function Game()
chats = {} chats = {}
local chat_x = math.floor(math.random(love.graphics.getWidth() )) for i = 1, 1 do
local chat_x = math.floor(math.random(love.graphics.getWidth()))
local chat_y = math.floor(math.random(love.graphics.getHeight())) local chat_y = math.floor(math.random(love.graphics.getHeight()))
table.insert(chats, i, Chat(chat_x, chat_y, self.level))
table.insert(chats, 1, Chat(chat_x, chat_y, self.level)) end
table.insert(chats, 2, Chat(chat_x, chat_y, self.level))
table.insert(chats, 3, Chat(chat_x, chat_y, self.level))
table.insert(chats, 4, Chat(chat_x, chat_y, self.level))
end end
} }

2
Player.lua

@ -39,7 +39,6 @@ function Player(debugging)
end end
end, end,
destroyBan = function (self, index) destroyBan = function (self, index)
print(self.ban, index)
table.remove(self.bans, index) table.remove(self.bans, index)
end, end,
@ -50,7 +49,6 @@ function Player(debugging)
if love.keyboard.isDown('left') then if love.keyboard.isDown('left') then
self.angle = self.angle - self.rotation_speed * dt self.angle = self.angle - self.rotation_speed * dt
self.facing = self.angle self.facing = self.angle
print(self.facing)
end end
if love.keyboard.isDown('right') then if love.keyboard.isDown('right') then
self.angle = self.angle + self.rotation_speed * dt self.angle = self.angle + self.rotation_speed * dt

BIN
assets/f1065sk1.pdf

Binary file not shown.

2
main.lua

@ -64,7 +64,7 @@ function love.update(dt)
end end
end end
chat:move(dt) chat:move(player.x, player.y, dt)
end end
elseif game.state.menu then elseif game.state.menu then

Loading…
Cancel
Save