Add UserEvent events class. Only event currently is when user has manually placed a block.

This commit is contained in:
UnknownShadow200 2016-05-09 00:26:36 +10:00
parent 5633b61fd9
commit 3fa57fa6ac
5 changed files with 62 additions and 18 deletions

View file

@ -165,6 +165,7 @@
<Compile Include="Entities\NetPlayer.cs" />
<Compile Include="Events\EntityEvents.cs" />
<Compile Include="Events\Events.cs" />
<Compile Include="Events\UserEvents.cs" />
<Compile Include="Events\WorldEvents.cs" />
<Compile Include="Game\Game.Properties.cs" />
<Compile Include="Game\PickingHandler.cs" />

View file

@ -0,0 +1,38 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
namespace ClassicalSharp.Events {
public class UserEvents {
/// <summary> Raised when the user changes a block in the world. </summary>
public event EventHandler<BlockChangedEventArgs> BlockChanged;
internal void RaiseBlockChanged( Vector3I coords, byte old, byte block ) {
blockArgs.Coords = coords; blockArgs.OldBlock = old; blockArgs.Block = block;
Raise( BlockChanged, blockArgs );
}
BlockChangedEventArgs blockArgs = new BlockChangedEventArgs();
protected void Raise( EventHandler handler ) {
if( handler != null )
handler( this, EventArgs.Empty );
}
protected void Raise<T>( EventHandler<T> handler, T args ) where T : EventArgs {
if( handler != null )
handler( this, args );
}
}
public sealed class BlockChangedEventArgs : EventArgs {
/// <summary> Location within the world the block was updated at. </summary>
public Vector3I Coords;
/// <summary> Block ID that was at the given location before. </summary>
public byte OldBlock;
/// <summary> Block ID that is now at the given location. </summary>
public byte Block;
}
}

View file

@ -105,6 +105,7 @@ namespace ClassicalSharp {
public OtherEvents Events = new OtherEvents();
public EntityEvents EntityEvents = new EntityEvents();
public WorldEvents WorldEvents = new WorldEvents();
public UserEvents UserEvents = new UserEvents();
public InputHandler InputHandler;
public Chat Chat;
public BlockHandRenderer BlockHandRenderer;

View file

@ -13,7 +13,7 @@ namespace ClassicalSharp {
public PickingHandler( Game game, InputHandler input ) {
this.game = game;
this.input = input;
}
}
internal DateTime lastClick = DateTime.MinValue;
public void PickBlocks( bool cooldown, bool left, bool middle, bool right ) {
@ -32,47 +32,51 @@ namespace ClassicalSharp {
}
int buttonsDown = (left ? 1 : 0) + (right ? 1 : 0) + (middle ? 1 : 0);
if( buttonsDown > 1 || game.ActiveScreen.HandlesAllInput ||
if( buttonsDown > 1 || game.ActiveScreen.HandlesAllInput ||
inv.HeldBlock == Block.Air ) return;
// always play delete animations, even if we aren't picking a block.
if( left ) game.BlockHandRenderer.SetAnimationClick( true );
if( !game.SelectedPos.Valid ) return;
BlockInfo info = game.BlockInfo;
if( middle ) {
Vector3I pos = game.SelectedPos.BlockPos;
byte block = 0;
if( game.World.IsValidPos( pos ) && (block = game.World.GetBlock( pos )) != 0
&& (inv.CanPlace[block] || inv.CanDelete[block]) ) {
if( !game.World.IsValidPos( pos ) ) return;
byte old = game.World.GetBlock( pos );
if( !info.IsAir[old] && (inv.CanPlace[old] || inv.CanDelete[old]) ) {
for( int i = 0; i < inv.Hotbar.Length; i++ ) {
if( inv.Hotbar[i] == (Block)block ) {
if( inv.Hotbar[i] == (Block)old ) {
inv.HeldBlockIndex = i; return;
}
}
inv.HeldBlock = (Block)block;
inv.HeldBlock = (Block)old;
}
} else if( left ) {
Vector3I pos = game.SelectedPos.BlockPos;
byte block = 0;
if( game.World.IsValidPos( pos ) && (block = game.World.GetBlock( pos )) != 0
&& inv.CanDelete[block] ) {
game.ParticleManager.BreakBlockEffect( pos, block );
game.AudioPlayer.PlayDigSound( game.BlockInfo.DigSounds[block] );
if( !game.World.IsValidPos( pos ) ) return;
byte old = game.World.GetBlock( pos );
if( !info.IsAir[old] && inv.CanDelete[old] ) {
game.ParticleManager.BreakBlockEffect( pos, old );
game.AudioPlayer.PlayDigSound( game.BlockInfo.DigSounds[old] );
game.UpdateBlock( pos.X, pos.Y, pos.Z, 0 );
game.Network.SendSetBlock( pos.X, pos.Y, pos.Z, false, (byte)inv.HeldBlock );
game.UserEvents.RaiseBlockChanged( pos, old, 0 );
}
} else if( right ) {
Vector3I pos = game.SelectedPos.TranslatedPos;
if( !game.World.IsValidPos( pos ) ) return;
byte old = game.World.GetBlock( pos );
byte block = (byte)inv.HeldBlock;
if( !game.CanPick( game.World.GetBlock( pos ) ) && inv.CanPlace[block]
&& CheckIsFree( game.SelectedPos, block ) ) {
if( !game.CanPick( old ) && inv.CanPlace[block] && CheckIsFree( game.SelectedPos, block ) ) {
game.UpdateBlock( pos.X, pos.Y, pos.Z, block );
game.AudioPlayer.PlayDigSound( game.BlockInfo.StepSounds[block] );
game.Network.SendSetBlock( pos.X, pos.Y, pos.Z, true, block );
game.BlockHandRenderer.SetAnimationClick( false );
game.UserEvents.RaiseBlockChanged( pos, old, block );
}
}
}

View file

@ -52,10 +52,10 @@ namespace ClassicalSharp {
width = 640; height = 480;
if( device.Width >= 1024 && device.Height >= 768 ) {
//width = 800; height = 600;
width = 800; height = 600;
}
if( device.Width >= 1920 && device.Height >= 1080 ) {
//width = 1600; height = 900;
width = 1600; height = 900;
}
}