Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Commit

Permalink
Partially implement sand, fully implement stairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevault committed Feb 11, 2015
1 parent c190f83 commit 4f66305
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 8 deletions.
3 changes: 3 additions & 0 deletions TrueCraft.API/Entities/IEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using TrueCraft.API.Networking;
using TrueCraft.API.Server;
using TrueCraft.API.World;

namespace TrueCraft.API.Entities
{
Expand All @@ -16,6 +17,8 @@ public interface IEntity : INotifyPropertyChanged
DateTime SpawnTime { get; set; }
MetadataDictionary Metadata { get; }
Size Size { get; }
IEntityManager EntityManager { get; set; }
IWorld World { get; set; }
bool SendMetadataToClients { get; }
void Update(IEntityManager entityManager);
}
Expand Down
3 changes: 3 additions & 0 deletions TrueCraft.Core/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using TrueCraft.API;
using TrueCraft.API.Networking;
using TrueCraft.API.Server;
using TrueCraft.API.World;

namespace TrueCraft.Core.Entities
{
Expand All @@ -22,6 +23,8 @@ protected Entity()
public DateTime SpawnTime { get; set; }

public int EntityID { get; set; }
public IEntityManager EntityManager { get; set; }
public IWorld World { get; set; }

protected Vector3 _Position;
public virtual Vector3 Position
Expand Down
94 changes: 94 additions & 0 deletions TrueCraft.Core/Entities/FallingSandEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using TrueCraft.API;
using TrueCraft.API.Networking;
using TrueCraft.Core.Networking.Packets;
using TrueCraft.API.Entities;
using TrueCraft.API.Server;
using TrueCraft.API.World;
using TrueCraft.Core.Logic.Blocks;

namespace TrueCraft.Core.Entities
{
public class FallingSandEntity : ObjectEntity, IAABBEntity
{
public FallingSandEntity(Vector3 position)
{
_Position = position + new Vector3(0.5);
}

public override byte EntityType { get { return 70; } }

public override Size Size
{
get
{
return new Size(1);
}
}

public override IPacket SpawnPacket
{
get
{
return new SpawnGenericEntityPacket(EntityID, (sbyte)EntityType,
MathHelper.CreateAbsoluteInt(Position.X), MathHelper.CreateAbsoluteInt(Position.Y),
MathHelper.CreateAbsoluteInt(Position.Z), 0, null, null, null);
}
}

public override int Data { get { return 1; } }

public void TerrainCollision(Vector3 collisionPoint, Vector3 collisionDirection)
{
if (collisionDirection == Vector3.Down)
{
EntityManager.DespawnEntity(this);
World.SetBlockID((Coordinates3D)_Position, SandBlock.BlockID);
}
}

public BoundingBox BoundingBox
{
get
{
return new BoundingBox(Vector3.Zero, Vector3.One);
}
}

public bool BeginUpdate()
{
EnablePropertyChange = false;
return true;
}

public void EndUpdate(Vector3 newPosition)
{
EnablePropertyChange = true;
Position = newPosition;
}

public float AccelerationDueToGravity
{
get
{
return 0.4f;
}
}

public float Drag
{
get
{
return 0.2f;
}
}

public float TerminalVelocity
{
get
{
return 1.96f;
}
}
}
}
19 changes: 19 additions & 0 deletions TrueCraft.Core/Logic/Blocks/SandBlock.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System;
using TrueCraft.API.Logic;
using TrueCraft.API.Server;
using TrueCraft.API.World;
using TrueCraft.API;
using TrueCraft.Core.Entities;
using TrueCraft.API.Networking;

namespace TrueCraft.Core.Logic.Blocks
{
Expand All @@ -21,5 +26,19 @@ public class SandBlock : BlockProvider
{
return new Tuple<int, int>(2, 1);
}

public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{
BlockUpdate(descriptor, descriptor, user.Server, world);
}

public override void BlockUpdate(BlockDescriptor descriptor, BlockDescriptor source, IMultiplayerServer server, IWorld world)
{
if (world.GetBlockID(descriptor.Coordinates + Coordinates3D.Down) == AirBlock.BlockID)
{
world.SetBlockID(descriptor.Coordinates, AirBlock.BlockID);
server.GetEntityManagerForWorld(world).SpawnEntity(new FallingSandEntity(descriptor.Coordinates));
}
}
}
}
10 changes: 5 additions & 5 deletions TrueCraft.Core/Logic/Blocks/StoneStairsBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace TrueCraft.Core.Logic.Blocks
{
public class StoneStairsBlock : BlockProvider, ICraftingRecipe
public class StoneStairsBlock : WoodenStairsBlock, ICraftingRecipe
{
public static readonly byte BlockID = 0x43;
public static new readonly byte BlockID = 0x43;

public override byte ID { get { return 0x43; } }

Expand All @@ -22,7 +22,7 @@ public class StoneStairsBlock : BlockProvider, ICraftingRecipe

public override string DisplayName { get { return "Stone Stairs"; } }

public ItemStack[,] Pattern
public override ItemStack[,] Pattern
{
get
{
Expand All @@ -35,15 +35,15 @@ public class StoneStairsBlock : BlockProvider, ICraftingRecipe
}
}

public ItemStack Output
public override ItemStack Output
{
get
{
return new ItemStack(BlockID);
}
}

public bool SignificantMetadata
public override bool SignificantMetadata
{
get
{
Expand Down
40 changes: 37 additions & 3 deletions TrueCraft.Core/Logic/Blocks/WoodenStairsBlock.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
using System;
using TrueCraft.API.Logic;
using TrueCraft.API;
using TrueCraft.API.World;
using TrueCraft.API.Networking;

namespace TrueCraft.Core.Logic.Blocks
{
public class WoodenStairsBlock : BlockProvider, ICraftingRecipe
{
public enum StairDirection
{
East = 0,
West = 1,
South = 2,
North = 3
}

public static readonly byte BlockID = 0x35;

public override byte ID { get { return 0x35; } }
Expand All @@ -22,7 +32,7 @@ public class WoodenStairsBlock : BlockProvider, ICraftingRecipe

public override string DisplayName { get { return "Wooden Stairs"; } }

public ItemStack[,] Pattern
public virtual ItemStack[,] Pattern
{
get
{
Expand All @@ -35,20 +45,44 @@ public class WoodenStairsBlock : BlockProvider, ICraftingRecipe
}
}

public ItemStack Output
public virtual ItemStack Output
{
get
{
return new ItemStack(BlockID);
}
}

public bool SignificantMetadata
public virtual bool SignificantMetadata
{
get
{
return false;
}
}

public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{
byte meta = 0;
switch (MathHelper.DirectionByRotationFlat(user.Entity.Yaw))
{
case Direction.East:
meta = (byte)StairDirection.East;
break;
case Direction.West:
meta = (byte)StairDirection.West;
break;
case Direction.North:
meta = (byte)StairDirection.North;
break;
case Direction.South:
meta = (byte)StairDirection.South;
break;
default:
meta = 0; // Should never happen
break;
}
world.SetMetadata(descriptor.Coordinates, meta);
}
}
}
14 changes: 14 additions & 0 deletions TrueCraft.Core/Networking/Packets/SpawnGenericEntityPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ public struct SpawnGenericEntityPacket : IPacket
{
public byte ID { get { return 0x17; } }

public SpawnGenericEntityPacket(int entityID, sbyte entityType, int x, int y, int z,
int data, short? xVelocity, short? yVelocity, short? zVelocity)
{
EntityID = entityID;
EntityType = entityType;
X = x;
Y = y;
Z = z;
Data = data;
XVelocity = xVelocity;
YVelocity = yVelocity;
ZVelocity = zVelocity;
}

public int EntityID;
public sbyte EntityType; // TODO: Enum? Maybe a lookup would be better.
public int X, Y, Z;
Expand Down
1 change: 1 addition & 0 deletions TrueCraft.Core/TrueCraft.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<Compile Include="Networking\Packets\CloseWindowPacket.cs" />
<Compile Include="Logic\Items\ArmorItem.cs" />
<Compile Include="Logic\Blocks\FluidBlock.cs" />
<Compile Include="Entities\FallingSandEntity.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions TrueCraft/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ public IList<IRemoteClient> ClientsForEntity(IEntity entity)
public void SpawnEntity(IEntity entity)
{
entity.SpawnTime = DateTime.Now;
entity.EntityManager = this;
entity.World = World;
entity.EntityID = NextEntityID++;
entity.PropertyChanged -= HandlePropertyChanged;
entity.PropertyChanged += HandlePropertyChanged;
Expand Down
1 change: 1 addition & 0 deletions TrueCraft/PhysicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void Update()
double multipler = (DateTime.Now - LastUpdate).TotalMilliseconds / MillisecondsBetweenUpdates;
if (LastUpdate == DateTime.MinValue)
multipler = 1;
multipler *= 0.3; // HACKY FIX
lock (EntityLock)
{
foreach (var entity in Entities)
Expand Down

0 comments on commit 4f66305

Please sign in to comment.