-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
195 lines (160 loc) · 4.9 KB
/
Copy pathmain.lua
File metadata and controls
195 lines (160 loc) · 4.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
function love.load()
--camera
require "camera"
excessAtEdgeOfScreen = 100
zoom = 1
vector = require 'hump.vector'
-- graphics love
g = love.graphics
-- Game world
love.physics.setMeter(30)
world = love.physics.newWorld( 0, 0, true )
--These callback function names can be almost any you want:
world:setCallbacks(beginContact)
gameover = false
-- instantiate our player and set initial values
require "Player"
p = Player:new()
sRelp = ""
admin = false
map = false
require "entities/SolarSystem"
s = SolarSystem:new()
-- SOUNDS
damage = love.audio.newSource("Sounds/damage.wav","static")
-- IMAGES
--bkgrnd = g.newImage("imgs/nightsky.jpg")
compass = g.newImage('imgs/compass.png')
end
function love.update(dt)
--update World
world:update(dt)
-- update Player
p:update(dt)
-- update Solar System
s:update(dt)
if p.shape:getRadius() < 50 then
zoom = 0.25 --zoom - 0.1
elseif p.shape:getRadius() < 100 then
zoom = 0.5 --zoom + 0.1
elseif p.shape:getRadius() < 150 then
zoom = 1 --zoom - 0.1
end
if love.keyboard.isDown("a") then
admin = true
end
if love.keyboard.isDown("lshift") then
-- if map == true then map = false
-- else map = true end
end
if love.keyboard.isDown("r") then
p = nil
p = Player:new()
s = nil
s = SolarSystem:new()
camera:setPosition(0, 0)
zoom = 0.25
end
-- determine sun position relative to player
sx = s.sun.body:getX( )
sy = s.sun.body:getY( )
px = p.body:getX( )
py = p.body:getY( )
if sy >= py then
-- South
if sx >= px then sRelp = "SE"
else sRelp = "SW" end
else
-- North
if sx >= px then sRelp = "NE"
else sRelp = "NW" end
end
end
function love.draw()
g.setFont( g.newFont(14) )
--g.draw(bkgrnd,0 - bkgrnd:getWidth()/2,0 - bkgrnd:getHeight()/2)
if gameover then
g.setColor(255, 255, 255) -- white
g.print("GAME OVER", g.getWidth()/2 - 40, 150)
g.print("Press r to restart", g.getWidth()/2 - 50, 200)
end
camera:setScale(zoom,zoom)
g.setColor(255, 255, 255) -- white
--ADMIN
if admin then
g.print("FPS: " .. love.timer.getFPS(), 2, 2)
g.print("Zoom level: " .. zoom, 2, 22)
g.print("Number of Asteroids: " .. #s.asteroids, 2, 42)
end
camera:set()
-- Draw player
p:draw()
-- Draw entities
s:draw()
camera:unset()
-- HUD
-- COMPASS
--g.print("The Sun is " ..sRelp, g.getWidth() - 400, 400)
g.draw(compass, g.getWidth() - 200, 150, 0, 0.25, 0.25)
center = g.getWidth() - 120
if sRelp == 'NW' then g.line (center, 230, center - 70, 170)
elseif sRelp == 'NE' then g.line (center, 230, center + 70, 170)
elseif sRelp == 'SW' then g.line (center, 230, center - 70, 310)
elseif sRelp == 'SE' then g.line (center, 230, center + 70, 310) end
-- Health
for i=1,p.health,1 do
-- Heart
love.graphics.setColor(255, 0, 0) -- red
g.arc( "fill", (g.getWidth() - (70 * p.health) - 200) + 62.5 + 70*i, 50, 12.5, math.pi, 2*math.pi)
g.arc( "fill", (g.getWidth() - (70 * p.health) - 200) + 87.5 + 70*i, 50, 12.5, math.pi, 2*math.pi)
g.polygon('fill', (g.getWidth() - (70 * p.health) - 200) + 50 + 70*i, 50, (g.getWidth() - (70 * p.health) - 200) + 100 + 70*i, 50, (g.getWidth() - (70 * p.health) - 200) + 75 + 70*i, 100)
end
-- Score
g.setColor(255, 255, 255) -- white
g.setFont( g.newFont(20) )
score = math.floor(((p.shape:getRadius()*math.pi)/2) * 1000)
g.print("Size: " .. score, 10, 10)
--MAP
if map then
g.setColor(213, 213, 213)
g.rectangle ("fill", p.body:getX()/10, p.body:getY()/10, 10,10)
g.setColor(155, 155, 155)
for i,v in ipairs(s.planets) do
x = (v.body:getX() - (p.body:getX()))/10
y = (v.body:getY() - (p.body:getY()))/10
g.rectangle ("fill", x, y, 10,10)
end
end
end
function beginContact(a, b, coll)
x,y = coll:getNormal()
-- Two of the same type colliding
if a:getUserData() == b:getUserData() then
-- Mark asteroid to be removed
if a:getUserData() == 'Asteroid' then
if a:getShape():getRadius() > b:getShape():getRadius() then
b:setUserData("DESTROYME")
else
a:setUserData("DESTROYME")
end
end
end
-- The player colliding with something
if a:getUserData() == "Player" then
sizeOfPlayer = p.shape:getRadius()
object = b:getShape():getRadius()
if object > sizeOfPlayer then
p.health = p.health - 1
love.audio.stop(damage)
love.audio.play(damage)
else
newR = math.sqrt(((math.pi * (sizeOfPlayer*sizeOfPlayer))+(math.pi * (object*object)))/math.pi)
a:getShape():setRadius(newR)
p.shape:setRadius(newR)
p:draw()
b:setUserData("DESTROYME")
-- Absorb the object
--b:setUserData('PlayerSTICKIES')
end
end
end