Skip to content

Commit

Permalink
dummy ai added... AIStarter.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
mhml92 committed Feb 3, 2015
1 parent 421e8e2 commit b9d036a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ function Factory:createPlayer(x,y,r,controller)
p:addComponent(Graphics:new(p,self.rm:getImg("spaceship"),40))

-- COOLDOWN, FORCE
--p:addComponent(BasicWeapon:new(20,500))
p:addComponent(Shotgun:new(40,5*500))
p:addComponent(BasicWeapon:new(20,500))
--p:addComponent(Shotgun:new(40,5*500))
p:addComponent(PlayerExit:new())
p:addComponent(LovePhysicsScreenFix:new(p,0))
p:addComponent(Crosshair:new(self.rm:getImg("cross")))
Expand Down
8 changes: 5 additions & 3 deletions GameState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ local KeyboardController= require 'components/KeyboardController'

-- AI
local VokronAI = require 'components/VokronAI'
local VokronAIalt = require 'components/VokronAI-alt'
local VokronAIalt = require 'components/VokronAI-alt'
local SOSAI = require 'components/EnemyController'
local AIStarter = require 'components/AIStarter'

function GameState:initialize()

Expand All @@ -38,10 +39,11 @@ end

function GameState:startGame()
self.objmgr:clear()
--self:addPlayer(GamePadController)
self:addPlayer(AIStarter)
self:addPlayer(GamePadController)
--self:addPlayer(KeyboardController)
--self:addPlayer(VokronAIalt)
self:addPlayer(VokronAIalt)
--self:addPlayer(VokronAIalt)
--for i = 1,3 do
-- self:addEnemy(SOSAI)
--end
Expand Down
63 changes: 63 additions & 0 deletions components/AIStarter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,72 @@ function AIStarter:getMoveDir()
dir.x = 0
dir.y = 0

local nearest = {}
nearest.v = nil
nearest.x = 9999999
nearest.y = 9999999
for k,v in ipairs(self.astroids) do
if nearest.v == nil then
nearest.v = v
end
nearest = self:setNearest(nearest,v)
end

dir.x = self.p.trans:getX() - nearest.x
dir.y = self.p.trans:getY() - nearest.y
dir.len = math.sqrt(dir.x^2+dir.y^2)
dir.x = (dir.x/(dir.len))
dir.y = (dir.y/(dir.len))
return dir
end

function AIStarter:setNearest(n,v)
local d1,d2,d3,d4,d5,t
local w,h = love.graphics.getDimensions()
t = v.trans
local dists ={}

d1 = {}
d1.x = t:getX()
d1.y = t:getY()
d1.d = self:dist(d1.x,d1.y)
table.insert(dists,d1)
d2 = {}
d2.x = t:getX()-w
d2.y = t:getY()
d2.d = self:dist(d2.x,d2.y)
table.insert(dists,d2)
d3 = {}
d3.x = t:getX()+w
d3.y = t:getY()
d3.d = self:dist(d3.x,d3.y)
table.insert(dists,d3)
d4 = {}
d4.x = t:getX()
d4.y = t:getY()-h
d4.d = self:dist(d4.x,d4.y)
table.insert(dists,d4)
d5 = {}
d5.x = t:getX()
d5.y = t:getY()+h
d5.d = self:dist(d5.x,d5.y)
table.insert(dists,d5)

table.sort(dists,function(a,b) return a.d < b.d end)

if self:dist(n.x,n.y) > dists[1].d then
n.v = v
n.x = dists[1].x
n.y = dists[1].y
end
return n
end


function AIStarter:dist(x,y)
return math.sqrt((self.p.trans:getX()-x)^2 + (self.p.trans:getY()-y)^2)
end

function AIStarter:getShootDir()
--A vector of any length (except 0) pointing in the shooting direction
--a vector of length 0 will result in no shooting
Expand Down
2 changes: 1 addition & 1 deletion conf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function love.conf(t)
t.window.minheight = 1 -- Minimum window height if the window is resizable (number)
t.window.fullscreen = true--false -- Enable fullscreen (boolean)
t.window.fullscreentype = "normal"--"desktop" -- Standard (normal) fullscreen or desktop fullscreen mode (string)
t.window.vsync =true -- Enable vertical sync (boolean)
t.window.vsync =false--true -- Enable vertical sync (boolean)
t.window.fsaa = 1 -- The number of samples to use with multi-sampled antialiasing (number)
t.window.display = 1 -- Index of the monitor to show the window in (number)
t.window.highdpi = false -- Enable high-dpi mode for the window on a Retina display (boolean). Added in 0.9.1
Expand Down

0 comments on commit b9d036a

Please sign in to comment.