-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
219 lines (185 loc) · 6.36 KB
/
main.lua
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
local scale = 20 -- number of pixels between each sample
local chunkSize = 100 -- in pixels
local seed = math.floor(love.timer.getTime()) -- change seed for different terrain gen
local noiseScale = 0.1 -- scale at which to generate noise
local playerSpeed = 200
local wireframe = false -- toggles wire frame display on/off
local persistantWindow = false
-- to see changes generate terrain ("r")
running = {}
local timer1 = 0
local fps = 0
-- local shift = 10
local numV = math.floor(chunkSize/scale) + 1
local numH = math.floor(chunkSize/scale) + 1
local terrain
local saveLoad = require "Modules/saveLoad"
local camXOffset = 0
local camYOffset = 0
local zoom = 1
local qx, qy = 300,300
local screenWidth = 1280
local screenHeight = 800
local player = {
t = nil,
x = 0,
y = 0,
r = 13,
theta = 0,
thetaV = 3,
v = playerSpeed
}
function love.load()
love.keyboard.setKeyRepeat(true)
love.graphics.setDefaultFilter("nearest", "nearest")
love.math.setRandomSeed(seed)
Object = require "Libraries/classic/classic"
require "Libraries/collisions"
require "Modules/valueSquare"
require "Modules/terrainChunk"
require "Modules/fluidAutomata"
require "Modules/terrain"
local terrainTexture = love.graphics.newImage("Assets/mesh.png")
local waterTexture = love.graphics.newImage("Assets/water.png")
player.t = love.graphics.newImage("Assets/nautilus.png")
timer1 = love.timer.getTime()
terrain = Terrain(terrainTexture, scale, chunkSize, noiseScale, love.math.random()*1000000, love.math.random()*1000000)
-- love.graphics.setBackgroundColor(51/255, 26/255, 51/255)
end
local test = 0
function love.update(delta)
fps = love.timer.getFPS()
if(love.mouse.isDown(1)) then
terrain:addPoint()
end
if(love.mouse.isDown(2)) then
terrain:removePoint()
end
if (#running > 0 and running[1]:update()) then
table.remove(running, 1)
if (#running == 0) then
print("t: "..love.timer.getTime()-timer1)
end
end
if (love.keyboard.isDown("a") or love.keyboard.isDown("left")) then
player.theta = player.theta - player.thetaV*delta
end
if (love.keyboard.isDown("d") or love.keyboard.isDown("right")) then
player.theta = player.theta + player.thetaV*delta
end
if (love.keyboard.isDown("s") or love.keyboard.isDown("down")) then
-- camYOffset = camYOffset - delta*300*math.sqrt(math.abs(zoom))
-- terrain:updatePosition(camXOffset, camYOffset)
local prevX = player.x
local prevY = player.y
player.x = player.x + player.v*delta*math.cos(player.theta)
-- if(terrain:resolveColision(player.x, player.y, player.r)) then
-- player.x = prevX
-- end
player.y = player.y + player.v*delta*math.sin(player.theta)
-- if(terrain:resolveColision(player.x, player.y, player.r)) then
-- player.y = prevY
-- end
qx, qy = terrain:resolveColision(player.x, player.y, player.r, player.v)
elseif (love.keyboard.isDown("w") or love.keyboard.isDown("up")) then
-- camYOffset = camYOffset + delta*300*math.sqrt(math.abs(zoom))
-- terrain:updatePosition(camXOffset, camYOffset)
local prevX = player.x
local prevY = player.y
player.x = player.x - player.v*delta*math.cos(player.theta)
-- if(terrain:resolveColision(player.x, player.y, player.r)) then
-- player.x = prevX
-- end
player.y = player.y - player.v*delta*math.sin(player.theta)
-- if(terrain:resolveColision(player.x, player.y, player.r)) then
-- player.y = prevY
-- end
-- player.x, player.y = terrain:resolveColision(player.x, player.y, player.r)
-- if (terrain:resolveColision(player.x, player.y, player.r)) then
-- end
qx, qy = terrain:resolveColision(player.x, player.y, player.r, player.v)
-- qx, qy = 0, 0
if (qx and qy) then
-- player.x = prevX
-- player.y = prevY
local Δx = qx - player.x
local Δy = qy - player.y
local d = math.sqrt((Δx)^2 + (Δy)^2)
if(d < player.r) then
-- player.x = prevX
-- player.y = prevY
-- print(Δx, Δy)
local Δx = qx - prevX
local Δy = qy - prevY
local d = math.sqrt((Δx)^2 + (Δy)^2)
d = (d - player.r)/d
-- if not (player.r/d == 1/0) then
player.x = player.x + Δx*d
player.y = player.y + Δy*d
-- end
end
end
-- print(qx, qy)
-- print(terrain:resolveColision(player.x, player.y, player.r))
end
if not (persistantWindow) then
love.window.setPosition(screenWidth/2 - love.graphics.getWidth()/2 + player.x, screenHeight/2 - love.graphics.getHeight()/2 + player.y)
end
camYOffset = love.graphics.getHeight()/2-player.y
camXOffset = love.graphics.getWidth()/2-player.x
terrain:updatePosition(camXOffset, camYOffset)
end
function love.draw()
love.graphics.setWireframe(wireframe)
love.graphics.translate(camXOffset, camYOffset)
if (zoom < 0) then love.graphics.scale(-1/zoom, -1/zoom) else
love.graphics.scale(zoom, zoom)
end
terrain:draw()
-- terrain:drawPoints()
-- water:drawPoints()
-- water:draw()
love.graphics.setColor(1.0, 1.0, 1.0)
-- 35, 23
love.graphics.draw(player.t, player.x, player.y, player.theta, 1, 1, player.t:getWidth()/2, player.t:getHeight()/2)
love.graphics.setColor(0.0, 1.0, 0.1)
terrain:drawOutline()
love.graphics.circle("line", player.x, player.y, player.r)
love.graphics.setPointSize(2)
-- love.graphics.points(qx, qy)
love.graphics.setWireframe(false)
love.graphics.origin()
love.graphics.print(fps, 0, 0)
love.graphics.print(math.floor(camXOffset)..", "..math.floor(camYOffset), 0, 20)
love.graphics.print(zoom, 0, 40)
local windowWidth, windowHeight = love.graphics.getWidth(), love.graphics.getHeight()
local x, y = love.mouse.getX(), love.mouse.getY()
love.graphics.setColor(x/windowWidth, y/windowHeight, 1-x/windowWidth, 1)
local x = math.floor(x/scale)*scale
local y = math.floor(y/scale)*scale
-- love.graphics.circle("line", x, y, 0.5*scale/2)
-- love.graphics.points(player.x-4 + player.r+4, player.y+2 + player.r-2)
end
function love.keypressed(key)
if key == "r" then
local xShift = love.math.random()*1000000
local yShift = love.math.random()*1000000
if (#running == 0) then
timer1 = love.timer.getTime()
terrain:generate(xShift, yShift)
end
elseif key == "=" then
zoom = zoom + 1
camXOffset = camXOffset - love.graphics.getHeight()/2
camYOffset = camYOffset - love.graphics.getWidth()/2
elseif key == "-" then
zoom = zoom - 1
-- camXOffset = camXOffset + love.graphics.getHeight()/4
-- camYOffset = camYOffset + love.graphics.getWidth()/4
end
end
function love.quit()
-- saveLoad.save(terrain[1])
end
function love.resize()
end