-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Blocks can now be Spawned and Removed. Used when it's placed/destro…
…yed by the server, not by the player. * Added basic block physics features - falling sand and gravel. See #52, #94
- Loading branch information
Gremlin13
authored and
Gremlin13
committed
Oct 10, 2011
1 parent
6db76bc
commit b02aa64
Showing
8 changed files
with
255 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Chraft.Net.Packets; | ||
using Chraft.Utils; | ||
|
||
namespace Chraft.World.Blocks.Physics | ||
{ | ||
public abstract class BlockBasePhysics | ||
{ | ||
public int EntityId { get; protected set; } | ||
public WorldManager World { get; protected set; } | ||
public Location Position { get; protected set; } | ||
public bool IsPlaying { get; protected set; } | ||
public Vector3 Velocity { get; protected set; } | ||
public AddObjectVehiclePacket.ObjectType Type; | ||
|
||
protected BlockBasePhysics(WorldManager world, Location pos) | ||
{ | ||
World = world; | ||
Position = pos; | ||
EntityId = world.Server.AllocateEntity(); | ||
|
||
CreateEntityPacket entity = new CreateEntityPacket { EntityId = EntityId }; | ||
foreach (var nearbyPlayer in World.Server.GetNearbyPlayers(World, new AbsWorldCoords(Position.X, Position.Y, Position.Z))) | ||
{ | ||
nearbyPlayer.SendPacket(entity); | ||
} | ||
} | ||
|
||
public virtual void Start() | ||
{ | ||
if (IsPlaying) | ||
return; | ||
AddObjectVehiclePacket obj = new AddObjectVehiclePacket | ||
{ | ||
EntityId = EntityId, | ||
Type = Type, | ||
UnknownFlag = 0, | ||
X = Position.X, | ||
Y = Position.Y, | ||
Z = Position.Z | ||
}; | ||
foreach (var nearbyPlayer in World.Server.GetNearbyPlayers(World, new AbsWorldCoords(Position.X, Position.Y, Position.Z))) | ||
{ | ||
nearbyPlayer.SendPacket(obj); | ||
} | ||
IsPlaying = true; | ||
} | ||
|
||
public virtual void Simulate() | ||
{ | ||
} | ||
|
||
public virtual void Stop() | ||
{ | ||
IsPlaying = false; | ||
BlockBasePhysics unused = null; | ||
World.PhysicsBlocks.TryRemove(EntityId, out unused); | ||
DestroyEntityPacket entity = new DestroyEntityPacket { EntityId = EntityId }; | ||
foreach (var nearbyPlayer in World.Server.GetNearbyPlayers(World, new AbsWorldCoords(Position.X, Position.Y, Position.Z))) | ||
{ | ||
nearbyPlayer.SendPacket(entity); | ||
} | ||
OnStop(); | ||
} | ||
|
||
protected virtual void OnStop() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Chraft.Utils; | ||
|
||
namespace Chraft.World.Blocks.Physics | ||
{ | ||
public class FallingGravel : FallingSand | ||
{ | ||
public FallingGravel(WorldManager world, Location pos) : base(world, pos) | ||
{ | ||
Type = Net.Packets.AddObjectVehiclePacket.ObjectType.FallingGravel; | ||
BlockId = (byte) BlockData.Blocks.Gravel; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Chraft.Utils; | ||
|
||
namespace Chraft.World.Blocks.Physics | ||
{ | ||
public class FallingSand : BlockBasePhysics | ||
{ | ||
protected byte BlockId; | ||
|
||
public FallingSand(WorldManager world, Location pos) : base(world, pos) | ||
{ | ||
Type = Net.Packets.AddObjectVehiclePacket.ObjectType.FallingSand; | ||
BlockId = (byte) BlockData.Blocks.Sand; | ||
Velocity = new Vector3(0, -0.4D, 0); | ||
} | ||
|
||
public override void Simulate() | ||
{ | ||
int x = MathHelper.floor_double(Position.X); | ||
int y = MathHelper.floor_double(Position.Y); | ||
int z = MathHelper.floor_double(Position.Z); | ||
byte blockId = World.GetBlockId(x, y, z); | ||
if (blockId != (byte)BlockData.Blocks.Air) | ||
{ | ||
Stop(); | ||
return; | ||
} | ||
|
||
if (Position.Y <= 1) | ||
{ | ||
Stop(); | ||
return; | ||
} | ||
|
||
Position.Vector += Velocity; | ||
} | ||
|
||
protected override void OnStop() | ||
{ | ||
UniversalCoords uc = UniversalCoords.FromWorld(MathHelper.floor_double(Position.X), MathHelper.floor_double(Position.Y) + 1, MathHelper.floor_double(Position.Z)); | ||
StructBlock block = new StructBlock(uc, BlockId, 0, World); | ||
BlockHelper.Instance(BlockId).Spawn(block); | ||
base.OnStop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters