Skip to content

Commit

Permalink
add enemies automaticly
Browse files Browse the repository at this point in the history
  • Loading branch information
apoh committed Dec 29, 2011
1 parent 6b3a81e commit fa02d0c
Showing 1 changed file with 48 additions and 28 deletions.
76 changes: 48 additions & 28 deletions space/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,7 @@ function Player:update()
end

if wall.input[2].left then
local speed = 0.05
local start_x = R(5, 10) * shuffle({-1,1})[1]
local start_y = R(5, 10) * shuffle({-1,1})[1]

local enemy = Enemy {
source = env.enemys,
origin = env.player,
x = start_x,
y = start_y,
dir = Vector({ x = (R()-0.5), y = (R()-0.5) }):norm():mul(speed),
}
env.enemys[enemy] = enemy
add_enemy()
end

for key, projectile in pairs(self.projectiles) do
Expand Down Expand Up @@ -245,7 +234,7 @@ function Target:init(opts)
opts = opts or {}
self.coords = Vector(opts)
self.color = opts.color or hex(0, 180, 0)
self.away_color = opts.away_color or hex(0, 60, 0)
self.away_color = opts.away_color or hex(0, 100, 0)
self.source = opts.source
if not self.source then
error("no source given")
Expand Down Expand Up @@ -277,7 +266,7 @@ function Enemy:init(opts)
self.dir = Vector(opts.dir)
self.radius = opts.radius or 3
self.color = opts.color or hex(180, 0, 0)
self.away_color = opts.away_color or hex(60, 0, 0)
self.away_color = opts.away_color or hex(100, 0, 0)
self.flash_color = opts.flash_color or hex(180, 60, 0)
self.source = opts.source
if not self.source then
Expand All @@ -290,10 +279,11 @@ function Enemy:init(opts)
end

function Enemy:update()
if self.flash == 0 then return end
if self.flash == nil then
for _, target in ipairs(env.targets or {}) do
if self.coords:eq(target.coords, 0.1) then
self.flash = 10
self.flash = 30
break
end
end
Expand All @@ -313,11 +303,13 @@ function Enemy:update()

if self.flash == 0 then
self.source[self] = nil
env.enemys.length = env.enemys.length - 1
end
end
end

function Enemy:draw()
if self.flash == 0 then return end
local pos = self.coords:clone():sub(self.origin.coords):add(self.origin.pos)
local x,y
x = inbound(pos.x, 1, wall.width) - 1
Expand All @@ -344,6 +336,16 @@ function Enemy:draw()
wall:pixel(floor(x), floor(y), color)
end
end
function Enemy:addEnemy()
local enemy = Enemy {
source = env.enemys,
origin = env.player,
x = start_x,
y = start_y,
dir = Vector({ x = (R()-0.5), y = (R()-0.5) }):norm():mul(speed),
}
env.enemys[enemy] = enemy
end


--------------------------------------------------------------------------------
Expand Down Expand Up @@ -420,8 +422,14 @@ function update()
end
env.targets.sum = sum

for _, enemy in pairs(env.enemys or {}) do
enemy:update()
for key, enemy in pairs(env.enemys or {}) do
if key ~= "length" then
enemy:update()
end
end

if env.enemys.length == 0 then
add_enemy()
end

tick = tick + 1
Expand All @@ -441,18 +449,37 @@ function draw()
target:draw()
end

for _, enemy in pairs(env.enemys or {}) do
enemy:draw()
for key, enemy in pairs(env.enemys or {}) do
if key ~= "length" then
enemy:draw()
end
end

env.player:draw()

end

function add_enemy()
local speed = 0.05
local start_x = R(5, 10) * shuffle({-1,1})[1]
local start_y = R(5, 10) * shuffle({-1,1})[1]

local enemy = Enemy {
source = env.enemys,
origin = env.player,
x = start_x,
y = start_y,
dir = Vector({ x = (R()-0.5), y = (R()-0.5) }):norm():mul(speed),
}
env.enemys[enemy] = enemy
env.enemys.length = env.enemys.length + 1
end

--------------------------------------------------------------------------------

function love.load()
wall = Wall(false, 1338, 3, false) -- "176.99.24.251"
--wall = Wall('176.99.24.251', 1338, 3, false) -- "176.99.24.251"

-- __maxd = math.sqrt(wall.width*wall.height)

Expand Down Expand Up @@ -495,15 +522,8 @@ function love.load()
y = 0,
}

env.enemys = setmetatable({}, { __mode = 'k'})
local enemy = Enemy {
source = env.enemys,
origin = env.player,
x = 3,
y = 3,
dir = {x=-0.05, y=-0.05},
}
env.enemys[enemy] = enemy
env.enemys = setmetatable({length=0}, { __mode = 'k'})
add_enemy()
end


Expand Down

0 comments on commit fa02d0c

Please sign in to comment.