Skip to content

Commit

Permalink
add death and respawning
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveH355 committed Jun 9, 2023
1 parent ad3cbf0 commit 25dd0de
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 89 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/com/dave/astronomer/MeloAstronomer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.badlogic.gdx.physics.box2d.Box2D;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxNativesLoader;
import com.dave.astronomer.client.GameSkin;
import com.dave.astronomer.client.asset.AssetFinder;
import com.dave.astronomer.client.screen.SplashScreen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ public void render(float delta) {

//camera follow player
MainPlayer player = GameState.getInstance().getMainPlayer();
Vector3 target = new Vector3(player.getPosition(), 0);


Vector3 target = new Vector3(player.getPosition(), 0);

if (player.getDashKey().isDown()) {
camera.position.lerp(target, delta * 6);
Expand All @@ -188,7 +187,7 @@ public void render(float delta) {
camera.translate(CameraShake.getShake());
}

if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT) && !player.isDead()) {

Vector3 worldCoords = camera.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ public static SpriteComponent createSpriteComponent() {
int dashDuration = 100;
Animation<TextureRegion> dash = AnimationUtils.loadFromTexture(texture, width, height, 8, 3, 0, dashDuration);

int deathDuration = 100;
Animation<TextureRegion> death = AnimationUtils.loadFromTexture(texture, width, height, 8, 7, 0, deathDuration);


Map<String, Animation<TextureRegion>> map = new HashMap<>();
map.put("idle", idle);
map.put("walk", walk);
map.put("dash", dash);
map.put("death", death);


SpriteComponent component = new SpriteComponent(map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public MainPlayerSystem() {

@Override
public void processEntity(MainPlayer p, float delta) {
if (p.isDead()) return;


Vector2 velocity = new Vector2();

float speed = p.getEntityType().speed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ public class InputComponent extends BaseComponent implements InputProcessor {

public static class KeyAction {
@Getter private int key;
@Getter @Setter boolean isDown;
@Setter boolean isDown;
@Getter @Setter boolean disabled;

public boolean isDown() {
if (disabled) return false;
else return isDown;
}
public KeyAction(int key) {
this.key = key;
}
Expand All @@ -25,6 +31,9 @@ public void addKeyAction(KeyAction... actions) {
keyMap.put(action.key, action);
}
}
public void disableAll() {
keyMap.values().forEach(keyAction -> keyAction.setDisabled(true));
}

@Override
public boolean keyDown(int keycode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ public AbstractClientPlayer(CoreEngine engine, UUID uuid) {
}
@Override
public void hurt() {
super.hurt();
takingDamage = true;
flashTimer = 0;
}

@Override
public void die() {
super.die();
getSpriteComponent().selectAnimationIfAbsent("death", false);
takingDamage = false;
getSpriteComponent().getSprite().setColor(Color.WHITE);
}

@Override
public void update(float delta) {
super.update(delta);

if (takingDamage) {
if (takingDamage && !isDead()) {
flashTimer += delta;
if (flashTimer > flashDuration) {
flashTimer = 0f;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dave.astronomer.client.world.entity;

import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.physics.box2d.Body;
import com.dave.astronomer.client.temp.TempPlayerAnimation;
import com.dave.astronomer.client.world.CameraShake;
Expand All @@ -14,20 +15,18 @@
import java.util.UUID;

public class MainPlayer extends AbstractClientPlayer {
@Getter private SpriteComponent spriteComponent;
@Getter private Body body;
@Getter private InputComponent inputComponent;
@Getter private InputComponent.KeyAction walkUpKey, walkDownKey, walkLeftKey, walkRightKey, dashKey;



@Getter
private SpriteComponent spriteComponent;
@Getter
private InputComponent inputComponent;
@Getter
private InputComponent.KeyAction walkUpKey, walkDownKey, walkLeftKey, walkRightKey, dashKey;
public MainPlayer(CoreEngine engine, UUID uuid) {
super(engine, uuid);


spriteComponent = createSpriteComponent();
inputComponent = createInputComponent();
body = PlayerData.createBody(engine.getSystem(PhysicsSystem.class).getWorld());


addComponents(
Expand All @@ -43,11 +42,22 @@ public void hurt() {
CameraShake.shake(0.03f, 0.25f);
}

@Override
public void die() {
super.die();
inputComponent.disableAll();
}

@Override
public void update(float delta) {
super.update(delta);
}

@Override
public Body createBody() {
return PlayerData.createBody(getEngine().getSystem(PhysicsSystem.class).getWorld());
}

public static SpriteComponent createSpriteComponent() {
return TempPlayerAnimation.createSpriteComponent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@
public class RemotePlayer extends AbstractClientPlayer {
@Getter
private SpriteComponent spriteComponent;
@Getter
private Body body;


public RemotePlayer(CoreEngine engine, UUID uuid) {
super(engine, uuid);
setMovementBehavior(new MovementBehavior.BasicLerp());

spriteComponent = MainPlayer.createSpriteComponent();
body = PlayerData.createBody(engine.getSystem(PhysicsSystem.class).getWorld());


addComponents(
spriteComponent
Expand All @@ -34,10 +29,16 @@ public RemotePlayer(CoreEngine engine, UUID uuid) {
@Override
public void update(float delta) {
super.update(delta);
if (isDead()) return;

spriteComponent.getSprite().setPosition(getPosition().x, getPosition().y);


MainPlayerSystem.determineAnimation(this, getDeltaSpeed());
}

@Override
public Body createBody() {
return PlayerData.createBody(getEngine().getSystem(PhysicsSystem.class).getWorld());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ private PlayerData(){}
public static Body createBody(World world) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;

bodyDef.fixedRotation = true;


Body b = world.createBody(bodyDef);

b.setUserData("player");


b.createFixture(PhysicsUtils.toShape(circle), 0);
b.createFixture(PhysicsUtils.toShape(circle), 100);

//hit box sensor
FixtureDef fdef = new FixtureDef();
fdef.isSensor = true;
fdef.shape = PhysicsUtils.toShape(rectangle);

fdef.shape = PhysicsUtils.toShape(rectangle);
b.createFixture(fdef);

return b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.badlogic.gdx.math.Vector2;
import com.dave.astronomer.client.multiplayer.ClientGamePacketHandler;
import com.dave.astronomer.common.world.entity.Player;

import java.util.UUID;

Expand All @@ -10,6 +11,11 @@ public class ClientboundAddPlayerPacket extends Packet<ClientGamePacketHandler>
public UUID uuid;
public Vector2 position;

public ClientboundAddPlayerPacket() {}
public ClientboundAddPlayerPacket(Player player) {
this.uuid = player.getUuid();
this.position = player.getPosition();
}
@Override
public void handle(ClientGamePacketHandler handler) {
handler.onAddPlayer(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ public abstract class BaseEntity extends Entity implements Disposable {
@Getter
private Vector2 deltaMovement;
@Getter float deltaSpeed;
@Getter private Body body;

public BaseEntity(EntityType<?> entityType, CoreEngine engine) {
this.engine = engine;
this.entityType = entityType;
this.body = createBody();
this.body.setUserData(this);
}

public void lerpPosition(Vector2 vector2, float speed) {
Expand All @@ -41,9 +44,6 @@ public void lerpPosition(Vector2 vector2, float speed) {

@Override
public void update(float delta) {
if (getBody().getUserData() != this) {
getBody().setUserData(this);
}
movementBehavior.apply(this);
}
public Packet<?> getAddEntityPacket() {
Expand All @@ -56,14 +56,14 @@ public void recreateFromPacket(ClientboundAddEntityPacket packet) {
}

public Vector2 getPosition() {
return getBody().getPosition();
return this.body.getPosition();
}

public void forcePosition(Vector2 position, float angle) {
getBody().setTransform(position, angle);
this.body.setTransform(position, angle);
}

public abstract Body getBody();
public abstract Body createBody();
//collision
public void beginCollision(CollisionContact contact, @CheckForNull BaseEntity entity) {

Expand All @@ -80,7 +80,7 @@ public void onRemovedFromEngine() {

@Override
public void dispose() {
getBody().getWorld().destroyBody(getBody());
this.body.getWorld().destroyBody(body);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.dave.astronomer.common.ashley.core.Engine;
import com.dave.astronomer.common.ashley.core.Entity;
import com.dave.astronomer.common.ashley.core.EntitySystem;
import lombok.Getter;
import com.dave.astronomer.common.world.entity.Player;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,6 +21,11 @@ public class CoreEngine extends Engine implements Disposable {
public CoreEngine(boolean isClientSide) {
this.isClientSide = isClientSide;
}

public void removeAndRespawnPlayer(Player player) {

}

public static EngineMetaData getEngineMetaData(CoreEngine engine) {
EngineMetaData data = new EngineMetaData();
for (EntitySystem system : engine.getSystems()) {
Expand Down
Loading

0 comments on commit 25dd0de

Please sign in to comment.