-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameState.lua
More file actions
178 lines (152 loc) · 5.67 KB
/
GameState.lua
File metadata and controls
178 lines (152 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
local class = require 'middleclass/middleclass'
local Factory = require 'Factory'
local GameObject = require 'GameObject'
local ResMgr = require 'ResourceManager'
local ObjectManager = require 'ObjectManager'
local CameraManager = require 'CameraManager'
local DebugManager = require 'DebugManager'
local GameState = class('GameState')
-- User Controllers
local AltGamePadController = require 'components/AltGamePadController'
local GamePadController = require 'components/GamePadController'
local KeyboardController = require 'components/KeyboardController'
local AltKeyboardController = require 'components/AltKeyboardController'
-- AI
local VokronAI = require 'AI/michael/VokronAI'
local Chaser = require 'AI/basic/Chaser'
local AIStarter = require 'AI/AIstarter/AIStarter'
function GameState:initialize()
self.hello = "yo gabba gabba"
self.resmgr = ResMgr:new(self)
self.objmgr = ObjectManager:new(self)
self.factory = Factory:new(self)
self.cammgr = CameraManager:new(self)
self.dbgmgr = DebugManager:new(self)
self.world = love.physics.newWorld(0,0,true)
self.world:setCallbacks(beginContact,endContact,preSolve,postSolve)
self:loadImages()
self:startGame()
end
function GameState:startGame()
self.objmgr:clear()
--self:addPlayer(AltGamePadController)
self:addPlayer(AltKeyboardController)
--self:addPlayer(AIStarter)
self.factory:createPlanet(0,0,math.pi/2,200, "testplanet")
--self.factory:createPlanet(0,786,math.pi/2,200, "testplanet3")
--self.factory:createPlanet(0,786*2,math.pi/2,200, "testplanet5")
--self.factory:createPlanet(1366,0,math.pi/2,200, "testplanet2")
--self.factory:createPlanet(1366,786,math.pi/2,200, "testplanet4")
--self.factory:createPlanet(1500,0,0,200)
end
function GameState:loadImages()
self.resmgr:loadImg('img/asteroids/asteroid3.png',"asteroid3")
self.resmgr:loadImg('img/asteroids/asteroid2.png',"asteroid2")
self.resmgr:loadImg('img/asteroids/asteroid1.png',"asteroid1")
self.resmgr:loadImg('img/ship2.png',"spaceship")
self.resmgr:loadImg('img/player.png',"booger")
self.resmgr:loadImg('img/shot.png',"fire")
self.resmgr:loadImg('img/saft.png',"saft")
self.resmgr:loadImg('img/fighter.png',"fighter")
self.resmgr:loadImg('img/bg.png',"bg")
self.resmgr:loadImg('img/floatbg1.png',"floatbg1")
self.resmgr:loadImg('img/floatbg2.png',"floatbg2")
self.resmgr:loadImg('img/floatbg3.png',"floatbg3")
self.resmgr:loadImg('img/hit.png','hit')
self.resmgr:loadImg('img/explosion.png','explosion')
self.resmgr:loadImg('img/crosshair.png','cross')
self.resmgr:loadImg('img/square.png','square')
self.resmgr:loadImg('img/planet/test.png','ptest')
self.resmgr:loadImg('img/planet/grav.png','grav')
self.resmgr:loadImg('img/planet/grav2.png','grav2')
self.resmgr:loadImg('img/planet/planet.png','ptest2')
self.resmgr:loadImg('img/planet/planet2.png','ptest3')
self.resmgr:loadImg('img/planet/shadow.png','pshadow')
self.resmgr:loadImg('img/planet/testplanet1.png','testplanet')
self.resmgr:loadImg('img/planet/testplanet2.png','testplanet2')
self.resmgr:loadImg('img/planet/testplanet3.png','testplanet3')
self.resmgr:loadImg('img/planet/testplanet4.png','testplanet4')
self.resmgr:loadImg('img/planet/testplanet5.png','testplanet5')
end
function GameState:update(dt)
self.dbgmgr:update(dt)
self.world:update(dt)
-- update
self.objmgr:updateAll()
local cpos = nil
if self.objmgr.players[1] ~= nil then
cpos = self.objmgr.players[1].trans
else
cpos = {}
cpos.x = 0
cpos.y = 0
end
self.cammgr:update(cpos.x,cpos.y)
end
function GameState:draw()
love.graphics.setBackgroundColor(34,34,38)
self.cammgr:attach()
local bgpos = nil
if self.objmgr.players[1] ~= nil then
bgpos = self.objmgr.players[1].trans
else
bgpos = {}
bgpos.x = 0
bgpos.y = 0
end
--self.cammgr.cam:rotateTo(-bgpos.r-math.pi/2)
self:drawBackground(bgpos.x,bgpos.y)
self.objmgr:drawAll()
self.cammgr:detach()
end
-- collision callbacks
function GameState:beginContact(a,b,coll)
self.objmgr:beginContact(a,b,coll)
end
function GameState:endContact(a,b,coll)
self.objmgr:endContact(a,b,coll)
end
function GameState:preSolve(a,b,coll)
self.objmgr:preSolve(a,b,coll)
end
function GameState:postSolve(a, b, coll, normalimpulse1, tangentimpulse1, normalimpulse2, tangentimpulse2)
self.objmgr:postSolve(a, b, coll, normalimpulse1, tangentimpulse1, normalimpulse2, tangentimpulse2)
end
function GameState:drawBackgroundLayer(x,y,img,distance)
local wh,hh = img:getWidth(),img:getHeight()
local w,h = wh*2,hh*2
local x1,y1 = math.floor(x/w), math.floor(y/h)
local dx,dy = (x/distance) % w,(y/distance) % h
local offx,offy = (x % w)-dx,(y % h)-dy
local Xc,Yc = (x1*w)+offx, (y1*h)+offy
local X1,X2 = Xc - wh, Xc + wh
local Y1,Y2 = Yc - hh, Yc + hh
love.graphics.draw(img,X1,Y1,0,2,2)
love.graphics.draw(img,X2,Y1,0,2,2)
love.graphics.draw(img,X1,Y2,0,2,2)
love.graphics.draw(img,X2,Y2,0,2,2)
end
function GameState:drawBackground(x,y)
--self:drawBackgroundLayer(x,y,self.resmgr:getImg('square'),6)
self:drawBackgroundLayer(x,y,self.resmgr:getImg('bg'),5)
self:drawBackgroundLayer(x,y,self.resmgr:getImg('floatbg2'),3.5)
self:drawBackgroundLayer(x,y,self.resmgr:getImg('floatbg1'),2.5)
self:drawBackgroundLayer(x,y,self.resmgr:getImg('floatbg3'),1)
end
function GameState:addPlayer(controller)
local w,h = love.graphics.getDimensions()
self.factory:createPlayer(w/2,h/2,0,controller)
end
function GameState:addAstroids(n)
for i = 1, n do
local x,y,r,level,size
local w, h = love.graphics.getDimensions()
x = math.random()*w
y = math.random()*h
r = math.random()*2*math.pi
size = 100
level = 3
self.factory:createAstroid(x,y,r,size,1,level)
end
end
return GameState