Skip to content

Commit

Permalink
Gravity
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Apr 21, 2024
1 parent fd9e415 commit 7192a73
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.github.xenfork.freeworld.world.block.BlockTypes;
import io.github.xenfork.freeworld.world.entity.Entity;
import io.github.xenfork.freeworld.world.entity.EntityTypes;
import io.github.xenfork.freeworld.world.entity.component.OnGroundComponent;
import org.joml.Vector2d;
import org.slf4j.Logger;
import overrun.marshal.Unmarshal;
Expand Down Expand Up @@ -176,18 +177,19 @@ private void onCursorPos(double x, double y) {

private void tick() {
camera.preUpdate();
final double speed = glfw.getKey(window, GLFW.KEY_LEFT_CONTROL) == GLFW.PRESS ? 1.0 : 0.5;
final boolean onGround = player.hasComponent(OnGroundComponent.ID);
double speed = onGround ? 0.1 : 0.07;
if (glfw.getKey(window, GLFW.KEY_LEFT_CONTROL) == GLFW.PRESS) speed *= 2.0;
double xo = 0.0;
double yo = 0.0;
double zo = 0.0;
if (glfw.getKey(window, GLFW.KEY_W) == GLFW.PRESS) zo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_S) == GLFW.PRESS) zo += 1.0;
if (glfw.getKey(window, GLFW.KEY_A) == GLFW.PRESS) xo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_D) == GLFW.PRESS) xo += 1.0;
if (glfw.getKey(window, GLFW.KEY_LEFT_SHIFT) == GLFW.PRESS) yo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) yo += 1.0;
player.acceleration().value().set(xo, yo, zo).mul(speed);
player.velocity().value().zero();
if (onGround && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) {
player.velocity().value().y = 0.5;
}
player.acceleration().value().set(xo, 0.0, zo).mul(speed);
world.tick();

if (blockDestroyTimer >= 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void setComponent(EntityComponent component) {
componentMap.put(component.componentId(), component);
}

public void removeComponent(Identifier id) {
componentMap.remove(id);
}

@SuppressWarnings("unchecked")
public <T extends EntityComponent> T getComponent(Identifier id) {
return (T) componentMap.get(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
* @author squid233
* @since 0.1.0
*/
public sealed interface EntityComponent permits BoundingBoxComponent, AccelerationComponent, EyeHeightComponent, PositionComponent, RotationXYComponent, VelocityComponent {
public sealed interface EntityComponent permits
AccelerationComponent,
BoundingBoxComponent,
EyeHeightComponent,
OnGroundComponent,
PositionComponent,
RotationXYComponent,
VelocityComponent {
/**
* {@return a unique identifier of this component}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* freeworld - 3D sandbox game
* Copyright (C) 2024 XenFork Union
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/

package io.github.xenfork.freeworld.world.entity.component;

import io.github.xenfork.freeworld.core.Identifier;

/**
* @author squid233
* @since 0.1.0
*/
public final class OnGroundComponent implements EntityComponent {
public static final Identifier ID = Identifier.ofBuiltin("on_ground");
public static final OnGroundComponent INSTANCE = new OnGroundComponent();

@Override
public Identifier componentId() {
return ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void process(World world, List<Entity> entities) {
velocity.add(acceleration);
MathUtil.moveRelative(velocity.x(), velocity.y(), velocity.z(), rotation.y(), movement);

AABBox boundingBox = null;
AABBox boundingBox;
if (entity.hasComponent(BoundingBoxComponent.ID)) {
boundingBox = entity.boundingBox().value();

Expand Down Expand Up @@ -73,9 +73,16 @@ public void process(World world, List<Entity> entities) {
}
}

final double originMovementY = movement.y();
for (AABBox box : boxes) {
movement.y = box.clipYCollide(boundingBox, movement.y());
}
if (originMovementY != movement.y && originMovementY < 0.0) {
velocity.y = 0.0;
entity.addComponent(OnGroundComponent.INSTANCE);
} else {
entity.removeComponent(OnGroundComponent.ID);
}
position.y += movement.y();
boundingBox = computeBox(boundingBox, position);

Expand All @@ -95,7 +102,8 @@ public void process(World world, List<Entity> entities) {
position.add(movement);
}

velocity.mul(0.5);
velocity.y -= 0.08;
velocity.mul(0.8, 0.98, 0.8);
}
}
}
Expand Down

0 comments on commit 7192a73

Please sign in to comment.