From 6db1175c95da8f01276d1047404fd725f7bdfc7a Mon Sep 17 00:00:00 2001 From: Anuken Date: Sun, 8 Apr 2018 12:02:42 -0400 Subject: [PATCH] Implemented conveyor physics --- core/assets/maps/trinity.mmap | Bin 198417 -> 198417 bytes core/assets/version.properties | 4 +- .../mindustry/content/blocks/Blocks.java | 5 ++- .../src/io/anuke/mindustry/entities/Unit.java | 14 +++++- core/src/io/anuke/mindustry/world/Block.java | 3 ++ .../blocks/types/distribution/Conveyor.java | 41 ++++++++++++++---- 6 files changed, 52 insertions(+), 15 deletions(-) diff --git a/core/assets/maps/trinity.mmap b/core/assets/maps/trinity.mmap index 42ee699faab5a71d0a169d0a0538a888e7844fd6..6c2104dfa3704045e7bf701c9c08bc5ecdb638d9 100644 GIT binary patch delta 740 zcmbQ($1|~yr(p}@HBWhA1_pd+vm*OLCMA3-Ao@2evfpOXAgm6q`YMwiv8v&EE)6q1 zbxhN15*b5C4+x0HC{?0DaI+$NHPiI>lNrrG1n2a%6BreVi_to;He--BT_O@nBUm}l w^tIC%RX}ycV4n!)@=RYljZp<824QVhV4ur0y|{oeZL 0.4f && Timers.get(this, "flooreffect", 14 - (velocity.len() * floor.speedMultiplier)*2f)){ + boolean onLiquid = floor.liquid; + + if(tile != null){ + tile.block().unitOn(tile, this); + if(tile.block() != Blocks.air){ + onLiquid = false; + } + } + + if(onLiquid && velocity.len() > 0.4f && Timers.get(this, "flooreffect", 14 - (velocity.len() * floor.speedMultiplier)*2f)){ Effects.effect(floor.walkEffect, floor.liquidColor, x, y); } @@ -66,7 +76,7 @@ public abstract class Unit extends SyncEntity { damagePeriodic(floor.damageTaken); } - if(floor.drownTime > 0){ + if(onLiquid && floor.drownTime > 0){ drownTime += Timers.delta() * 1f/floor.drownTime; if(Timers.get(this, "drowneffect", 15)){ Effects.effect(floor.drownUpdateEffect, floor.liquidColor, x, y); diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index 89d2df0455..1398ece612 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.reflect.ClassReflection; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.entities.TileEntity; +import io.anuke.mindustry.entities.Unit; import io.anuke.mindustry.graphics.DrawLayer; import io.anuke.mindustry.graphics.Layer; import io.anuke.mindustry.content.fx.ExplosionFx; @@ -122,6 +123,8 @@ public class Block extends BaseBlock { public void drawSelect(Tile tile){} public void drawPlace(int x, int y, int rotation, boolean valid){} public void placed(Tile tile){} + public void unitOn(Tile tile, Unit unit){} + /**Called after all blocks are created.*/ public void init(){ setStats(); diff --git a/core/src/io/anuke/mindustry/world/blocks/types/distribution/Conveyor.java b/core/src/io/anuke/mindustry/world/blocks/types/distribution/Conveyor.java index 644365809c..ec998f60c0 100644 --- a/core/src/io/anuke/mindustry/world/blocks/types/distribution/Conveyor.java +++ b/core/src/io/anuke/mindustry/world/blocks/types/distribution/Conveyor.java @@ -2,6 +2,7 @@ package io.anuke.mindustry.world.blocks.types.distribution; import com.badlogic.gdx.utils.LongArray; import io.anuke.mindustry.entities.TileEntity; +import io.anuke.mindustry.entities.Unit; import io.anuke.mindustry.resource.Item; import io.anuke.mindustry.content.Items; import io.anuke.mindustry.world.Block; @@ -33,7 +34,8 @@ public class Conveyor extends Block{ private final Translator tr1 = new Translator(); private final Translator tr2 = new Translator(); - public float speed = 0f; + protected float speed = 0f; + protected float carryCapacity = 8f; protected Conveyor(String name) { super(name); @@ -90,6 +92,26 @@ public class Conveyor extends Block{ } } + @Override + public void unitOn(Tile tile, Unit unit) { + ConveyorEntity entity = tile.entity(); + + float angle = tile.getRotation() * 90f; + float speed = this.speed * tilesize / 1.5f; + float tx = Angles.trnsx(angle, 1f), ty = Angles.trnsy(angle, 1f); + unit.velocity.add(tx * speed * Timers.delta(), ty * speed * Timers.delta()); + + if(Math.abs(tx) > Math.abs(ty)){ + float rx = tile.worldx() + tx/2f*tilesize; + entity.minCarry = Math.min(entity.minCarry, Mathf.clamp((entity.x - rx) * tx / tilesize)); + }else{ + float ry = tile.worldy() + ty/2f*tilesize; + entity.minCarry = Math.min(entity.minCarry, Mathf.clamp((entity.y - ry) * ty / tilesize)); + } + + entity.carrying += unit.getMass(); + } + @Override public void update(Tile tile){ @@ -97,6 +119,7 @@ public class Conveyor extends Block{ entity.minitem = 1f; int minremove = Integer.MAX_VALUE; + float speed = Math.max(this.speed - (1f - (carryCapacity - entity.carrying)/carryCapacity), 0f); for(int i = entity.convey.size - 1; i >= 0; i --){ long value = entity.convey.get(i); @@ -109,6 +132,9 @@ public class Conveyor extends Block{ } float nextpos = (i == entity.convey.size - 1 ? 100f : pos2.set(entity.convey.get(i + 1), ItemPos.updateShorts).y) - itemSpace; + if(entity.minCarry >= pos.y && entity.minCarry <= nextpos){ + nextpos = entity.minCarry; + } float maxmove = Math.min(nextpos - pos.y, speed * Timers.delta()); if(maxmove > minmove){ @@ -131,6 +157,9 @@ public class Conveyor extends Block{ } } + entity.carrying = 0f; + entity.minCarry = 2f; + if(minremove != Integer.MAX_VALUE) entity.convey.truncate(minremove); } @@ -175,18 +204,12 @@ public class Conveyor extends Block{ } } - /** - * Conveyor data format: - * [0] item ordinal - * [1] x: byte ranging from -128 to 127, scaled should be at [-1, 1], corresponds to relative X from the conveyor middle - * [2] y: byte ranging from -128 to 127, scaled should be at [0, 1], corresponds to relative Y from the conveyor start - * [3] seed: -128 to 127, unscaled - * Size is 4 bytes, or one int. - */ public static class ConveyorEntity extends TileEntity{ LongArray convey = new LongArray(); float minitem = 1; + float carrying; + float minCarry = 2f; @Override public void write(DataOutputStream stream) throws IOException{