Skip to content

Commit

Permalink
Merge pull request #66 from largato/basic_colision
Browse files Browse the repository at this point in the history
Basic colision
  • Loading branch information
luizcavalcanti authored Oct 26, 2018
2 parents ca331e4 + 5541784 commit 7c0cd72
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 84 deletions.
54 changes: 48 additions & 6 deletions character.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "assets"

local steer = require 'steer'

Character = Object:extend()

Character.LOYALTY_NONE = "none"
Expand All @@ -8,6 +10,7 @@ Character.LOYALTY_ENEMY = "enemy"
Character.LOYALTY_ALLY = "ally"

Character.VANISH_TIME = 3
Character.MAX_FRAMES_STUCK = 10

STATE_IDLE = 'idle'
STATE_MOVING = 'moving'
Expand All @@ -21,9 +24,9 @@ function Character:new(x, y, life, damage, loyalty)
self.max_life = life
self.damage = damage
self.loyalty = loyalty
self.box_height = 120
self.box_width = 120
self.bbox = Quad(x, y, 120, 30)
self.dead_for = 0
self.frames_stuck = 0
end

function Character:isDead()
Expand All @@ -50,8 +53,8 @@ function Character:isHealable()
end

function Character:clamp()
self.position.x = math.Clamp(self.position.x, self.box_width / 2, CONF_SCREEN_WIDTH - self.box_width / 2)
self.position.y = math.Clamp(self.position.y, self.box_height / 2, CONF_SCREEN_HEIGHT - self.box_height / 2)
self.position.x = math.Clamp(self.position.x, self.bbox.w/2, CONF_SCREEN_WIDTH - self.bbox.w/2)
self.position.y = math.Clamp(self.position.y, self.bbox.h/2, CONF_SCREEN_HEIGHT - self.bbox.h/2)
end


Expand All @@ -75,8 +78,13 @@ function Character:update(dt)
if self.sprite == nil then
return
end

self.sprite.x = self.position.x
self.sprite.y = self.position.y

self.bbox.x = self.position.x - (self.bbox.w/2)
self.bbox.y = self.position.y + (self.bbox.h)

local polar = self.velocity:toPolar()
if (polar.y ~= 0) then
self.sprite.flipX = math.sin(polar.x) < 0
Expand All @@ -93,9 +101,43 @@ function Character:draw(ox, oy)
-- Draw life bar
if not self:isDead() then
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", self.position.x - (self.box_width/2), self.position.y - (self.box_height/2), self.box_width, 5)
love.graphics.rectangle("fill", self.bbox.x, self.position.y - (self.bbox.h*2), self.bbox.w, 5)
love.graphics.setColor(0, 255, 0)
love.graphics.rectangle("fill", self.position.x - (self.box_width/2), self.position.y - (self.box_height/2), self.box_width * (self.life/self.max_life), 5)
love.graphics.rectangle("fill", self.bbox.x, self.position.y - (self.bbox.h*2), self.bbox.w * (self.life/self.max_life), 5)
end

-- Draw bounding box
-- if not self:isDead() then
-- love.graphics.setColor(1,0,0,0.5)
-- love.graphics.rectangle("fill", self.bbox.x, self.bbox.y, self.bbox.w, self.bbox.h)
-- end
end

function Character:move()
self:seek_target()
if not (self.target==nil) then
local distance = self.position:dist(self.target.position)
if distance > self.attack_distance then
local desired_velocity = steer.seek(self.position, self.target.position) * self.max_velocity
local steering = desired_velocity - self.velocity
self.velocity = self.velocity + steering

local new_position = self.position + self.velocity

local friends_list = self:getFriendsList()
for i, friend in ipairs(friends_list) do
if self ~= friend and self.bbox:collide(friend.bbox) then
self.frames_stuck = self.frames_stuck + 1
if self.frames_stuck < Character.MAX_FRAMES_STUCK then
return
end
end
end
self.position = new_position
self.frames_stuck = 0
else
self:changeState(STATE_LOADING)
end
end
end

Expand Down
28 changes: 23 additions & 5 deletions gamescene.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local COOLDOWN_TANK = 25
-- UI constants
local PLACEMENT_WIDTH = CONF_SCREEN_WIDTH / 16
local PLACEMENT_HEIGHT = CONF_SCREEN_HEIGHT / 9
local PLACEMENT_ROWS = 4
local PLACEMENT_ROWS = 8
local PLACEMENT_MOVE_INTERVAL = 0.1

-- Power bar
Expand Down Expand Up @@ -50,11 +50,14 @@ function GameScene:new()
CONF_SCREEN_HEIGHT / PLACEMENT_HEIGHT / 2 * PLACEMENT_HEIGHT)
-- Fonts
self.unit_card_font = assets.fonts.hemi_head_bd_it(18)

-- Placement status control
self.up_pressed = false
self.down_pressed = false
self.left_pressed = false
self.right_pressed = false
self.placement_elapsed = 0.0
self.placement_collided = false

-- Assets
self.img_button_a = love.graphics.newImage('assets/images/xb_a.png')
Expand Down Expand Up @@ -209,23 +212,33 @@ function GameScene:updatePlacement(dt)
self:movePlacementRight()
end
end

-- Check collision with player's chars
self.placement_collided = false
local q = Quad(self.placement_position.x, self.placement_position.y + (PLACEMENT_HEIGHT/2), PLACEMENT_WIDTH, PLACEMENT_HEIGHT)
for i, char in ipairs(gameworld_demonstrators) do
if q:collide(char.bbox) then
self.placement_collided = true
break
end
end
end

function GameScene:gamepadpressed(joystick, button)
if button == "a" then
if self:allowNewMelee() then
if self:allowNewMelee() and not self.placement_collided then
self:placeUnit(UNIT_TYPE_MELEE)
end
elseif button == "b" then
if self:allowNewGunner() then
if self:allowNewGunner() and not self.placement_collided then
self:placeUnit(UNIT_TYPE_GUNNER)
end
elseif button == "x" then
if self:allowNewMedic() then
if self:allowNewMedic() and not self.placement_collided then
self:placeUnit(UNIT_TYPE_MEDIC)
end
elseif button == "y" then
if self:allowNewTank() then
if self:allowNewTank() and not self.placement_collided then
self:placeUnit(UNIT_TYPE_TANK)
end
end
Expand Down Expand Up @@ -369,6 +382,11 @@ function GameScene:drawPlacementCursor()
corner_y = self.placement_position.y + PLACEMENT_HEIGHT
love.graphics.line(corner_x, corner_y, corner_x - 40, corner_y)
love.graphics.line(corner_x, corner_y, corner_x, corner_y - 40)

if self.placement_collided then
love.graphics.setColor(1, 0, 0, 0.5)
love.graphics.rectangle("fill", self.placement_position.x, self.placement_position.y, PLACEMENT_WIDTH, PLACEMENT_HEIGHT)
end
end

function GameScene:drawPowerBar()
Expand Down
15 changes: 0 additions & 15 deletions gunner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,6 @@ function Gunner:look()
end
end

function Gunner:move()
self:seek_target()
if not (self.target==nil) then
local distance = self.position:dist(self.target.position)
if distance > self.attack_distance then
local desired_velocity = steer.seek(self.position, self.target.position) * self.max_velocity
local steering = desired_velocity - self.velocity
self.velocity = self.velocity + steering
self.position = self.position + self.velocity
else
self:changeState(STATE_LOADING)
end
end
end

function Gunner:load()
if self.loading_timer >= LOAD_FRAMES then
self.loading_timer = 0
Expand Down
1 change: 1 addition & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CONF_SCREEN_HEIGHT = 1080

Object = require "libs/classic/classic"
Scene = require "scene"
Quad = require "quad"

require "steer"
require "utils"
Expand Down
34 changes: 10 additions & 24 deletions medic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ local HEALING_FRAMES = 60
function Medic:new(x, y, life, damage, loyalty)
Medic.super.new(self, x, y, life, damage, loyalty)
self.state = STATE_IDLE
self.patient = nil
self.target = nil

-- Motion
self.velocity = vector(0, 0)
self.max_velocity = 2.5

-- Distances
self.sight_distance = 1000
self.healing_distance = 20
self.attack_distance = 20 -- actually, healling distance

-- Timers
self.healing_timer = 0
Expand Down Expand Up @@ -63,49 +63,35 @@ function Medic:update(dt)
self.dead_for = self.dead_for + dt
end

self:clamp()
-- self:clamp()
end

function Medic:look()
self:seek_patient()
if not (self.patient == nil) then
self:seek_target()
if not (self.target == nil) then
self:changeState(STATE_MOVING)
self.sprite.flipX = self.patient.position.x < self.position.x
end
end

function Medic:move()
if not (self.patient==nil) then
local distance = self.position:dist(self.patient.position)
if distance > self.healing_distance then
local desired_velocity = steer.seek(self.position, self.patient.position) * self.max_velocity
local steering = desired_velocity - self.velocity
self.velocity = self.velocity + steering
self.position = self.position + self.velocity
else
self:changeState(STATE_HEALING)
end
self.sprite.flipX = self.target.position.x < self.position.x
end
end

function Medic:heal()
if self.healing_timer >= HEALING_FRAMES then
self.healing_timer = 0
self.patient:receiveDamage(self.damage)
self.target:receiveDamage(self.damage)
self:changeState(STATE_IDLE)
else
self.healing_timer = self.healing_timer + 1
end
end

function Medic:seek_patient()
self.patient = nil
function Medic:seek_target()
self.target = nil
local closer = self.sight_distance
for i, char in ipairs(self:getFriendsList()) do
local distance = self.position:dist(char.position)
if not(char==self) and distance < closer and not char:isDead() and char:isHurt() and char:isHealable() then
closer = distance
self.patient = char
self.target = char
print("Patient found: " .. distance)
end
end
Expand Down
20 changes: 1 addition & 19 deletions melee.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,6 @@ function Melee:look()
end
end

function Melee:move()
self:seek_target()
if not (self.target==nil) then
local distance = self.position:dist(self.target.position)
if distance > self.attack_distance then
local desired_velocity = steer.seek(self.position, self.target.position) * self.max_velocity
local steering = desired_velocity - self.velocity
self.velocity = self.velocity + steering
self.position = self.position + self.velocity
else
self:changeState(STATE_LOADING)
end
end
end

function Melee:load()
if self.loading_timer >= LOAD_FRAMES then
self.loading_timer = 0
Expand All @@ -117,10 +102,7 @@ function Melee:seek_target()
self.target = nil
local closer = self.sight_distance

local enemy_list = gameworld_demonstrators
if self.loyalty == Character.LOYALTY_USER then
enemy_list = gameworld_officers
end
local enemy_list = self:getEnemiesList()

for i, enemy in ipairs(enemy_list) do
local distance = self.position:dist(enemy.position)
Expand Down
15 changes: 15 additions & 0 deletions quad.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Quad = Object:extend()

function Quad:new(center_x, center_y, width, height)
self.x = center_x
self.y = center_y
self.w = width
self.h = height
end

function Quad:collide(other)
return math.abs(self.x - other.x) < (math.abs(self.w + other.w)/2) and
math.abs(self.y - other.y) < (math.abs(self.h + other.h)/2)
end

return Quad
15 changes: 0 additions & 15 deletions tank.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,6 @@ function Tank:look()
end
end

function Tank:move()
self:seek_target()
if not (self.target==nil) then
local distance = self.position:dist(self.target.position)
if distance > self.attack_distance then
local desired_velocity = steer.seek(self.position, self.target.position) * self.max_velocity
local steering = desired_velocity - self.velocity
self.velocity = self.velocity + steering
self.position = self.position + self.velocity
else
self:changeState(STATE_LOADING)
end
end
end

function Tank:load()
if self.loading_timer >= LOAD_FRAMES then
self.loading_timer = 0
Expand Down

0 comments on commit 7c0cd72

Please sign in to comment.