Skip to content

Commit

Permalink
Added a new type of enemy
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwater committed Nov 14, 2013
1 parent 9afec69 commit 4664752
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 162 deletions.
61 changes: 20 additions & 41 deletions base-ai.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,11 @@ BaseAI = class("BaseAI", CircleEntity)
-- @param y Y position
--
function BaseAI:initialize(x, y)
Entity.initialize(self, x, y, "dynamic", love.physics.newCircleShape(10))
self.speed = math.random(50, 65)
-- self.turnSspeed = 500
self.body:setLinearDamping(3)
self.fixture:setRestitution(1)
CircleEntity.initialize(self, x, y, "dynamic", 10)

self.fixture:setGroupIndex(colgroup.ENEMY)

self.nextFire = 0
self.fireInterval = 0.1

self.moveOffset = math.random(-20, 20) / 180 * math.pi

self.damage = 1

local ang = math.random(1, 360) / 180 * math.pi
self.body:applyLinearImpulse(math.cos(ang) * 10, math.sin(ang) * 10)

self.shield = Shield:new(self.body, 15, 1)
end


Expand All @@ -37,22 +23,7 @@ end
-- @param dt Time passed since last frame
--
function BaseAI:update(dt)
local dx = game.ship.body:getX() - self.body:getX()
local dy = game.ship.body:getY() - self.body:getY()
local ang = math.atan2(dy, dx) + self.moveOffset

self:aimInDirection(ang)
self.body:applyForce(math.cos(ang) * self.speed, math.sin(ang) * self.speed)


-- if self.nextFire <= love.timer.getTime() and math.random(1,200) == 1 then
-- self.nextFire = love.timer.getTime() + self.fireInterval

-- Bullet:new(self.body:getX(), self.body:getY(), self.body:getAngle(), 20, colgroup.ENEMY)
-- end


self.shield:update(dt)
end


Expand All @@ -61,13 +32,7 @@ end
-- Draws the BaseAI.
--
function BaseAI:draw()
if self.shield:isEmpty() then
love.graphics.setColor(200, 200, 200)
else
love.graphics.setColor(200, 200, 240)
end
love.graphics.circle("line", self.body:getX(), self.body:getY(), self.shape:getRadius(), self.shape:getRadius())
-- self.shield:draw()

end


Expand Down Expand Up @@ -97,12 +62,26 @@ end
-- Move the BaseAI into a specific direction
--
-- @param ang The angle of the direction (radians)
-- @param speedMul Speed multiplier, 1 is normal speed
-- @param speed Speed
--
function BaseAI:moveInDirection(ang, speed)
if not speed then speed = 1 end

self.body:applyForce(math.cos(ang) * speed, math.sin(ang) * speed)
end


---
-- BaseAI:pushInDirection
-- Pushes the BaseAI into a specific direction
--
-- @param ang The angle of the direction (radians)
-- @param force Force
--
function BaseAI:moveInDirection(ang, speedMul)
if not speedMul then speedMul = 1 end
function BaseAI:pushInDirection(ang, force)
if not force then force = 1 end

self.body:applyForce(math.cos(ang) * self.speed * speedMul, math.sin(ang) * self.speed * speedMul)
self.body:applyLinearImpulse(math.cos(ang) * force, math.sin(ang) * force)
end


Expand Down
70 changes: 70 additions & 0 deletions bute.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Bute = class("Bute", BaseAI)

---
-- Bute:initialize
-- An enemy that tries to shoot you.
--
-- @param x X position
-- @param y Y position
--
function Bute:initialize(x, y)
BaseAI.initialize(self, x, y)
self.speed = math.random(2, 5)
-- self.turnSspeed = 500
self.body:setLinearDamping(3)
self.fixture:setRestitution(1)

self.nextFire = 0
self.fireInterval = 0.1

self.moveOffset = math.random(-20, 20) / 180 * math.pi

self:pushInDirection(utils.randomAngle(), 10)

self.shield = Shield:new(self.body, 15, 2)
end


---
-- Bute:update
-- Updates the Bute.
--
-- @param dt Time passed since last frame
--
function Bute:update(dt)
local dx = game.ship.body:getX() - self.body:getX()
local dy = game.ship.body:getY() - self.body:getY()
local ang = math.atan2(dy, dx) + self.moveOffset

self:aimInDirection(ang)

if math.random(1, 600) > 500 + dt * 100 then
self:pushInDirection(utils.randomAngle(), self.speed)
end


if self.nextFire <= love.timer.getTime() and math.random(1,200) == 1 then
self.nextFire = love.timer.getTime() + self.fireInterval

Bullet:new(self.body:getX(), self.body:getY(), self.body:getAngle(), 20, colgroup.ENEMY)
end


self.shield:update(dt)
end


---
-- Bute:draw
-- Draws the Bute.
--
function Bute:draw()
if self.shield:isEmpty() then
love.graphics.setColor(200, 200, 200)
else
love.graphics.setColor(200, 200, 240)
end
love.graphics.circle("line", self.body:getX(), self.body:getY(), self.shape:getRadius(), self.shape:getRadius())

self.shield:draw()
end
2 changes: 0 additions & 2 deletions entity-spawner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ EntitySpawner = class("EntitySpawner")
-- @param amount Amount of entities to spawn. Also accepts a table {min, max} for randomized amount
--
function EntitySpawner:initialize(data)
for k,v in pairs(data) do print(k,v) end

self.x = data.x
self.y = data.y
self.entity = data.type
Expand Down
2 changes: 2 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function love.load()
require("bullet")
require("shield")
require("base-ai")
require("pont")
require("bute")
require("entity-spawner")
require("map")
require("ship")
Expand Down
55 changes: 55 additions & 0 deletions pont.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Pont = class("Pont", BaseAI)

---
-- Pont:initialize
-- An enemy that follows you around.
--
-- @param x X position
-- @param y Y position
--
function Pont:initialize(x, y)
BaseAI.initialize(self, x, y)
self.speed = math.random(50, 65)
-- self.turnSspeed = 500
self.body:setLinearDamping(3)
self.fixture:setRestitution(1)

self.moveOffset = math.random(-20, 20) / 180 * math.pi

local ang = math.random(1, 360) / 180 * math.pi
self.body:applyLinearImpulse(math.cos(ang) * 10, math.sin(ang) * 10)

self.shield = Shield:new(self.body, 15, 1)
end


---
-- Pont:update
-- Updates the Pont.
--
-- @param dt Time passed since last frame
--
function Pont:update(dt)
local dx = game.ship.body:getX() - self.body:getX()
local dy = game.ship.body:getY() - self.body:getY()
local ang = math.atan2(dy, dx) + self.moveOffset

self:moveInDirection(ang, self.speed)


self.shield:update(dt)
end


---
-- Pont:draw
-- Draws the Pont.
--
function Pont:draw()
if self.shield:isEmpty() then
love.graphics.setColor(200, 200, 200)
else
love.graphics.setColor(200, 200, 240)
end
love.graphics.circle("line", self.body:getX(), self.body:getY(), self.shape:getRadius(), self.shape:getRadius())
end
119 changes: 0 additions & 119 deletions ui-menu.lua

This file was deleted.

4 changes: 4 additions & 0 deletions utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ utils.screenToWorld = function (x, y)
y = y - game.cameray / game.zoom - love.window:getHeight() / 2
return x, y
end

utils.randomAngle = function ()
return math.random(1, 360) / 180 * math.pi
end

0 comments on commit 4664752

Please sign in to comment.