Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ss20:neg_sourcecode [2020/08/26 21:01] srather [Folder Structure:] |
ss20:neg_sourcecode [2020/09/11 14:25] (aktuell) srather [main] |
||
---|---|---|---|
Zeile 4: | Zeile 4: | ||
====== Sourcecode ====== | ====== Sourcecode ====== | ||
- | Download sourcecode from Git repository: | + | Download the release version here:\\ |
+ | {{:ss20:neg-world-engine.zip}} | ||
- | [[https://gitlab.tubit.tu-berlin.de/srather/NEG-World-Engine.git|https://gitlab.tubit.tu-berlin.de/srather/NEG-World-Engine.git]] | + | Or download the newest version from the Git repository:\\ |
+ | [[https://gitlab.tubit.tu-berlin.de/srather/NEG-World-Engine.git]] | ||
==== Folder Structure: ==== | ==== Folder Structure: ==== | ||
Zeile 75: | Zeile 77: | ||
* Move with WASD, the arrow keys or by clicking with your mouse | * Move with WASD, the arrow keys or by clicking with your mouse | ||
* Press the number keys 0-9 to explore different worlds | * Press the number keys 0-9 to explore different worlds | ||
- | * Have fun with your 7-40 fps | ||
- | [[neg_sourcecode#sourcecode|↑ Back to top]] | + | [[#top|↑ Back to top]] |
---- | ---- | ||
+ | |||
===== main ===== | ===== main ===== | ||
Zeile 110: | Zeile 112: | ||
current_fps = FPS | current_fps = FPS | ||
- | current_speed = Vector(0.0, 0.0) | + | current_speed = Vector(0, 0) |
# generate worlds | # generate worlds | ||
def gen4(): | def gen4(): | ||
+ | """ | ||
+ | Generate world 4 (and 8). | ||
+ | Return a big polygon with sides as mirrors and a | ||
+ | slightly smaller one with polygons on its vertices. | ||
+ | """ | ||
+ | |||
def mirror(m, v): | def mirror(m, v): | ||
v=Vector(*v) | v=Vector(*v) | ||
return 2 * v.dot(m) / m.dot(m) * m - v | return 2 * v.dot(m) / m.dot(m) * m - v | ||
- | |||
w = [] | w = [] | ||
pol = generate.polygon(4, (0, 0), 500) | pol = generate.polygon(4, (0, 0), 500) | ||
Zeile 128: | Zeile 135: | ||
def gen6(): | def gen6(): | ||
+ | """ | ||
+ | Generate additional objects for world 6. | ||
+ | Return the 2 houses with their doors. | ||
+ | """ | ||
+ | |||
w = [] | w = [] | ||
w += generate.border(generate.polygon(4, (0, 0), 200, phase=-0.5))[:-1] | w += generate.border(generate.polygon(4, (0, 0), 200, phase=-0.5))[:-1] | ||
Zeile 169: | Zeile 181: | ||
# 4: 4 mirrors | # 4: 4 mirrors | ||
[ | [ | ||
+ | |||
] | ] | ||
+ gen4(), | + gen4(), | ||
Zeile 238: | Zeile 251: | ||
] | ] | ||
- | |||
- | |||
- | world = [] | ||
def bake_physics(wrld): | def bake_physics(wrld): | ||
- | lns = [] | + | """ |
+ | Return a list of all lines an points in 'wrld' which have a collision. | ||
+ | """ | ||
+ | lns = [] | ||
for object in wrld: | for object in wrld: | ||
+ | # ignore floor tiles | ||
if type(object) is Floor: | if type(object) is Floor: | ||
continue | continue | ||
+ | # dissasample polygons into lines | ||
if isinstance(object, Polygon): | if isinstance(object, Polygon): | ||
if len(object) <= 2: | if len(object) <= 2: | ||
Zeile 255: | Zeile 270: | ||
for i, _ in enumerate(object): | for i, _ in enumerate(object): | ||
lns.append(object.__class__(object[i-1], object[i])) | lns.append(object.__class__(object[i-1], object[i])) | ||
- | |||
return lns | return lns | ||
- | |||
- | lines = bake_physics(world) | ||
Zeile 267: | Zeile 279: | ||
pygame.display.set_icon(pygame.image.load('icon_small.png')) | pygame.display.set_icon(pygame.image.load('icon_small.png')) | ||
clock = pygame.time.Clock() | clock = pygame.time.Clock() | ||
+ | world = [] # empty starting world | ||
+ | lines = bake_physics(world) | ||
Zeile 285: | Zeile 299: | ||
# check inputs | # check inputs | ||
- | force = Vector(0, 0) | + | keys = pygame.key.get_pressed() |
- | keys = pygame.key.get_pressed() # keyboard | + | # change world if numbers are pressed |
if keys[pygame.K_1]: world = worlds[0]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 2 | if keys[pygame.K_1]: world = worlds[0]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 2 | ||
if keys[pygame.K_2]: world = worlds[1]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 2 | if keys[pygame.K_2]: world = worlds[1]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 2 | ||
Zeile 298: | Zeile 312: | ||
if keys[pygame.K_9]: world = worlds[8]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 0 | if keys[pygame.K_9]: world = worlds[8]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 0 | ||
if keys[pygame.K_0]: world = worlds[9]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 1 | if keys[pygame.K_0]: world = worlds[9]; lines = bake_physics(world); POSITION *= 0; current_speed *= 0; DEPTH = 1 | ||
+ | |||
+ | # change speed if arrows are pressed | ||
+ | force = Vector(0, 0) | ||
if keys[pygame.K_LEFT ] or keys[pygame.K_a]: force += Vector(-1, 0) | if keys[pygame.K_LEFT ] or keys[pygame.K_a]: force += Vector(-1, 0) | ||
if keys[pygame.K_RIGHT] or keys[pygame.K_d]: force += Vector( 1, 0) | if keys[pygame.K_RIGHT] or keys[pygame.K_d]: force += Vector( 1, 0) | ||
Zeile 304: | Zeile 321: | ||
force = force.normalised() | force = force.normalised() | ||
- | if pygame.mouse.get_focused(): # mouse | + | # change speed if left mouse button is pressed |
- | if pygame.mouse.get_pressed()[0]: # left button pressed | + | if pygame.mouse.get_focused(): |
+ | if pygame.mouse.get_pressed()[0]: | ||
force += (Vector(*pygame.mouse.get_pos()) - WINDOW / 2).normalised() | force += (Vector(*pygame.mouse.get_pos()) - WINDOW / 2).normalised() | ||
+ | # apply changes (not opimal, movement should be independant of framerate) | ||
current_speed += force.normalised() * SPEED / max(current_fps, 0.1) | current_speed += force.normalised() * SPEED / max(current_fps, 0.1) | ||
current_speed *= SPEED ** -(1 / max(current_fps, 60)) | current_speed *= SPEED ** -(1 / max(current_fps, 60)) | ||
Zeile 313: | Zeile 332: | ||
POSITION += current_speed | POSITION += current_speed | ||
- | # check physics | + | # check physics (not optimal, player can glitch into things) |
for line in lines: | for line in lines: | ||
if line.dist_to(POSITION) < ZOOM/2: | if line.dist_to(POSITION) < ZOOM/2: | ||
Zeile 325: | Zeile 344: | ||
continue | continue | ||
+ | # removes the part of the speed vector which causes collision | ||
POSITION -= current_speed | POSITION -= current_speed | ||
current_speed = b_a * (b_a.dot(current_speed) / b_a.dot(b_a)) | current_speed = b_a * (b_a.dot(current_speed) / b_a.dot(b_a)) | ||
POSITION += current_speed | POSITION += current_speed | ||
- | #check if gone through portal | + | # check if gone through portal |
move = Ray(POSITION - current_speed, current_speed) | move = Ray(POSITION - current_speed, current_speed) | ||
for object in world: | for object in world: | ||
if type(object) is Portal: | if type(object) is Portal: | ||
+ | # intersect portal with movement ray | ||
port = Ray(object[0], object[1] - object[0]) | port = Ray(object[0], object[1] - object[0]) | ||
point = move.intersect(port) | point = move.intersect(port) | ||
if point: # not empty | if point: # not empty | ||
if move.value_at(*point) <= 1 and port.value_at(*point) <= 1: | if move.value_at(*point) <= 1 and port.value_at(*point) <= 1: | ||
+ | # change world to portal world | ||
world = object.go_through(move.dir, world) | world = object.go_through(move.dir, world) | ||
lines = bake_physics(world) | lines = bake_physics(world) | ||
Zeile 351: | Zeile 373: | ||
</file> | </file> | ||
- | [[neg_sourcecode#sourcecode|Back to top]] | + | [[#top|↑ Back to top]] |