Skip to content

Instantly share code, notes, and snippets.

@krzkrzkrz
Last active October 8, 2017 16:10
Show Gist options
  • Save krzkrzkrz/949897d0075584308c2aebca42ef0b52 to your computer and use it in GitHub Desktop.
Save krzkrzkrz/949897d0075584308c2aebca42ef0b52 to your computer and use it in GitHub Desktop.
local System = require('libs/system')
local function randomness_system()
local system = System.new { 'body', 'random_motion' }
function system:load(entity)
print 'in randomness system!'
end
function system:update(dt, entity)
local body = entity:get 'body'
local random_motion = entity:get 'random_motion'
-- Let villain move randomly
if random_motion.cooldown > 0 then
random_motion.cooldown = random_motion.cooldown - 1
-- Update the speed
body.speed = random_motion.speed
-- Whenever villain does a new movement, let villain fire
system:move(dt, body, random_motion)
system:fire()
else
movement_probability = {
'left',
'right',
'down',
'up',
'stop',
'towards_hero',
'towards_hero'
}
random_movement = movement_probability[math.random(1, 7)]
random_motion.movement = random_movement
random_motion.cooldown = random_motion.base[random_movement].cooldown
random_motion.speed = random_motion.base[random_movement].speed
end
end
-- function system:draw(entity)
-- end
-- local System = require('libs/system')
--
-- local function randomness_system()
-- -- local system = System.new { 'body', 'random_motion' }
-- local system = System.new { 'body' }
--
-- function system:load(entity)
-- print 'in randomness system!'
-- end
--
-- function system:update(dt, entity)
-- local body = entity:get 'body'
-- local random_motion = entity:get 'random_motion'
--
-- -- Let villain move randomly
-- if random_motion.cooldown > 0 then
-- random_motion.cooldown = random_motion.cooldown - 1
--
-- system:move(dt, body, random_motion.movement)
-- print('Moving randomly to ' .. randomly.movement)
--
-- -- Whenever villain does a new movement, let villain fire
-- -- self:fire()
--
-- else
-- movement_probability = {
-- 'left',
-- 'right',
-- 'down',
-- 'up',
-- 'stop',
-- -- 'towards_hero',
-- -- 'towards_hero'
-- }
--
-- random_movement = movement_probability[math.random(1, 7)]
-- random_motion.movement = random_movement
-- random_motion.cooldown = entity.base[random_movement].cooldown
-- random_movement.speed = entity.base[random_movement].speed
-- end
-- end
function system:move(dt, body, random_motion)
if random_motion.movement == 'right' then
body.x = body.x + (body.speed * dt)
elseif random_motion.movement == 'left' then
body.x = body.x - (body.speed * dt)
elseif random_motion.movement == 'down' then
body.y = body.y + (body.speed * dt)
elseif random_motion.movement == 'up' then
body.y = body.y - (body.speed * dt)
elseif random_motion.movement == 'stop' then
body.y = body.y
elseif random_motion.movement == 'towards_hero' then
-- If the villain is on left of hero
if body.x < random_motion.target_body.x then
body.x = body.x + (body.speed * dt)
end
-- If the villain is on right of hero
if body.x > random_motion.target_body.x then
body.x = body.x - (body.speed * dt)
end
-- If the villain is above the hero
if body.y < random_motion.target_body.y then
body.y = body.y + (body.speed * dt)
end
-- If the villain is below the hero
if body.y > random_motion.target_body.y then
body.y = body.y - (body.speed * dt)
end
end
end
function system:fire()
if self.slots.weapon and self.attack.cooldown.current <= 0 then
local weapon = self.slots.weapon
-- Reset the cooldown
self.attack.cooldown.current = self.attack.cooldown.base
local mx, my = (warrior.x + warrior.width - weapon.width), (warrior.y + warrior.height - weapon.height)
local angle = math.atan2(my - (self.y + self.height - weapon.height), mx - (self.x + self.width - weapon.width))
local dx, dy = math.cos(angle) * self.slots.weapon.speed,
math.sin(angle) * self.slots.weapon.speed
bullet = {
width = weapon.width,
height = weapon.height,
x = self.x + (self.width / 2) - (weapon.width / 2),
y = self.y + (self.height / 2) - (weapon.height / 2),
dx = dx,
dy = dy,
range = { max = weapon.max_range, current = 0 },
damage = math.random(self.attack.minimum, self.attack.maximum)
}
table.insert(weapon.bullets, bullet)
end
end
return system
end
return randomness_system
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment