Use generic BlockChanged event instead of hardcoding behaviour in PickingHandler.

This commit is contained in:
UnknownShadow200 2016-05-21 15:06:38 +10:00
parent f4b2403639
commit fba550f3c2
12 changed files with 46 additions and 24 deletions

View file

@ -1,6 +1,7 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT // ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System; using System;
using System.Threading; using System.Threading;
using ClassicalSharp.Events;
using SharpWave; using SharpWave;
using SharpWave.Codecs; using SharpWave.Codecs;
@ -31,7 +32,11 @@ namespace ClassicalSharp.Audio {
stepBoard.Init( "step_", files ); stepBoard.Init( "step_", files );
} }
public void Tick( double delta ) { void PlayBlockSound( object sender, BlockChangedEventArgs e ) {
if( e.Block == 0 )
PlayDigSound( game.BlockInfo.DigSounds[e.OldBlock] );
else
PlayDigSound( game.BlockInfo.StepSounds[e.Block] );
} }
public void PlayDigSound( SoundType type ) { PlaySound( type, digBoard ); } public void PlayDigSound( SoundType type ) { PlaySound( type, digBoard ); }

View file

@ -24,6 +24,7 @@ namespace ClassicalSharp.Audio {
SetMusic( game.UseMusic ); SetMusic( game.UseMusic );
game.UseSound = Options.GetBool( OptionsKey.UseSound, false ); game.UseSound = Options.GetBool( OptionsKey.UseSound, false );
SetSound( game.UseSound ); SetSound( game.UseSound );
game.UserEvents.BlockChanged += PlayBlockSound;
} }
public void Ready( Game game ) { } public void Ready( Game game ) { }
@ -96,6 +97,7 @@ namespace ClassicalSharp.Audio {
DisposeMusic(); DisposeMusic();
DisposeSound(); DisposeSound();
musicHandle.Close(); musicHandle.Close();
game.UserEvents.BlockChanged -= PlayBlockSound;
} }
void DisposeMusic() { void DisposeMusic() {

View file

@ -377,7 +377,6 @@ namespace ClassicalSharp {
Players.Tick( ticksPeriod ); Players.Tick( ticksPeriod );
ParticleManager.Tick( ticksPeriod ); ParticleManager.Tick( ticksPeriod );
Animations.Tick( ticksPeriod ); Animations.Tick( ticksPeriod );
AudioPlayer.Tick( ticksPeriod );
BlockHandRenderer.Tick( ticksPeriod ); BlockHandRenderer.Tick( ticksPeriod );
ticksThisFrame++; ticksThisFrame++;
ticksAccumulator -= ticksPeriod; ticksAccumulator -= ticksPeriod;

View file

@ -59,10 +59,7 @@ namespace ClassicalSharp {
byte old = game.World.GetBlock( pos ); byte old = game.World.GetBlock( pos );
if( !info.IsAir[old] && inv.CanDelete[old] ) { 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.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 ); game.UserEvents.RaiseBlockChanged( pos, old, 0 );
} }
} else if( right ) { } else if( right ) {
@ -73,9 +70,6 @@ namespace ClassicalSharp {
if( !game.CanPick( old ) && 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.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 ); game.UserEvents.RaiseBlockChanged( pos, old, block );
} }
} }

View file

@ -23,9 +23,6 @@ namespace ClassicalSharp {
/// <summary> Informs the server of the client's current position and orientation. </summary> /// <summary> Informs the server of the client's current position and orientation. </summary>
public abstract void SendPosition( Vector3 pos, float yaw, float pitch ); public abstract void SendPosition( Vector3 pos, float yaw, float pitch );
/// <summary> Informs the server that the client placed or deleted a block at the given coordinates. </summary>
public abstract void SendSetBlock( int x, int y, int z, bool place, byte block );
/// <summary> Informs the server that using the given mouse button, /// <summary> Informs the server that using the given mouse button,
/// the client clicked on a particular block or entity. </summary> /// the client clicked on a particular block or entity. </summary>
public abstract void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId, PickedPos pos ); public abstract void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId, PickedPos pos );

View file

@ -32,7 +32,7 @@ namespace ClassicalSharp.Network {
SendPacket(); SendPacket();
} }
public override void SendSetBlock( int x, int y, int z, bool place, byte block ) { void SendSetBlock( int x, int y, int z, bool place, byte block ) {
writer.WriteUInt8( (byte)Opcode.SetBlockClient ); writer.WriteUInt8( (byte)Opcode.SetBlockClient );
writer.WriteInt16( (short)x ); writer.WriteInt16( (short)x );
writer.WriteInt16( (short)y ); writer.WriteInt16( (short)y );

View file

@ -5,6 +5,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using ClassicalSharp.Entities; using ClassicalSharp.Entities;
using ClassicalSharp.Events;
using ClassicalSharp.Gui; using ClassicalSharp.Gui;
using ClassicalSharp.Network; using ClassicalSharp.Network;
using ClassicalSharp.TexturePack; using ClassicalSharp.TexturePack;
@ -50,6 +51,7 @@ namespace ClassicalSharp.Network {
receivedFirstPosition = false; receivedFirstPosition = false;
lastPacket = DateTime.UtcNow; lastPacket = DateTime.UtcNow;
game.WorldEvents.OnNewMap += OnNewMap; game.WorldEvents.OnNewMap += OnNewMap;
game.UserEvents.BlockChanged += BlockChanged;
MakeLoginPacket( game.Username, game.Mppass ); MakeLoginPacket( game.Username, game.Mppass );
SendPacket(); SendPacket();
@ -58,6 +60,7 @@ namespace ClassicalSharp.Network {
public override void Dispose() { public override void Dispose() {
game.WorldEvents.OnNewMap -= OnNewMap; game.WorldEvents.OnNewMap -= OnNewMap;
game.UserEvents.BlockChanged -= BlockChanged;
socket.Close(); socket.Close();
Disconnected = true; Disconnected = true;
} }
@ -206,6 +209,15 @@ namespace ClassicalSharp.Network {
Set( Opcode.CpeSetMapEnvProperty, HandleSetMapEnvProperty, 6 ); Set( Opcode.CpeSetMapEnvProperty, HandleSetMapEnvProperty, 6 );
} }
void BlockChanged( object sender, BlockChangedEventArgs e ) {
Vector3I p = e.Coords;
byte block = (byte)game.Inventory.HeldBlock;
if( e.Block == 0 )
SendSetBlock( p.X, p.Y, p.Z, false, block );
else
SendSetBlock( p.X, p.Y, p.Z, true, e.Block );
}
void OnNewMap( object sender, EventArgs e ) { void OnNewMap( object sender, EventArgs e ) {
// wipe all existing entity states // wipe all existing entity states
for( int i = 0; i < 256; i++ ) { for( int i = 0; i < 256; i++ ) {

View file

@ -21,6 +21,7 @@ namespace ClassicalSharp.Particles {
this.game = game; this.game = game;
vb = game.Graphics.CreateDynamicVb( VertexFormat.P3fT2fC4b, maxParticles * 4 ); vb = game.Graphics.CreateDynamicVb( VertexFormat.P3fT2fC4b, maxParticles * 4 );
game.Events.TerrainAtlasChanged += TerrainAtlasChanged; game.Events.TerrainAtlasChanged += TerrainAtlasChanged;
game.UserEvents.BlockChanged += BreakBlockEffect;
} }
public void Ready( Game game ) { } public void Ready( Game game ) { }
@ -120,6 +121,7 @@ namespace ClassicalSharp.Particles {
game.Graphics.DeleteDynamicVb( vb ); game.Graphics.DeleteDynamicVb( vb );
game.Graphics.DeleteTexture( ref ParticlesTexId ); game.Graphics.DeleteTexture( ref ParticlesTexId );
game.Events.TerrainAtlasChanged -= TerrainAtlasChanged; game.Events.TerrainAtlasChanged -= TerrainAtlasChanged;
game.UserEvents.BlockChanged -= BreakBlockEffect;
} }
void RemoveAt<T>( int index, T[] particles, ref int count ) where T : Particle { void RemoveAt<T>( int index, T[] particles, ref int count ) where T : Particle {

View file

@ -1,12 +1,17 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT // ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System; using System;
using ClassicalSharp.Events;
using OpenTK; using OpenTK;
namespace ClassicalSharp.Particles { namespace ClassicalSharp.Particles {
public partial class ParticleManager : IDisposable { public partial class ParticleManager : IDisposable {
public void BreakBlockEffect( Vector3I position, byte block ) { void BreakBlockEffect( object sender, BlockChangedEventArgs e ) {
if( e.Block != 0 ) return;
Vector3I position = e.Coords;
byte block = e.OldBlock;
Vector3 startPos = new Vector3( position.X, position.Y, position.Z ); Vector3 startPos = new Vector3( position.X, position.Y, position.Z );
int texLoc = game.BlockInfo.GetTextureLoc( block, Side.Left ), texIndex = 0; int texLoc = game.BlockInfo.GetTextureLoc( block, Side.Left ), texIndex = 0;
TextureRec baseRec = game.TerrainAtlas1D.GetTexRec( texLoc, 1, out texIndex ); TextureRec baseRec = game.TerrainAtlas1D.GetTexRec( texLoc, 1, out texIndex );

View file

@ -1,6 +1,7 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT // ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System; using System;
using ClassicalSharp.Entities; using ClassicalSharp.Entities;
using ClassicalSharp.Events;
using ClassicalSharp.GraphicsAPI; using ClassicalSharp.GraphicsAPI;
using ClassicalSharp.Model; using ClassicalSharp.Model;
using OpenTK; using OpenTK;
@ -28,6 +29,7 @@ namespace ClassicalSharp.Renderers {
fakeP = new FakePlayer( game ); fakeP = new FakePlayer( game );
lastType = (byte)game.Inventory.HeldBlock; lastType = (byte)game.Inventory.HeldBlock;
game.Events.HeldBlockChanged += HeldBlockChanged; game.Events.HeldBlockChanged += HeldBlockChanged;
game.UserEvents.BlockChanged += BlockChanged;
} }
public void Ready( Game game ) { } public void Ready( Game game ) { }
@ -186,6 +188,12 @@ namespace ClassicalSharp.Renderers {
public void Dispose() { public void Dispose() {
game.Events.HeldBlockChanged -= HeldBlockChanged; game.Events.HeldBlockChanged -= HeldBlockChanged;
game.UserEvents.BlockChanged -= BlockChanged;
}
void BlockChanged( object sender, BlockChangedEventArgs e ) {
if( e.Block == 0 ) return;
SetAnimationClick( false );
} }
} }

View file

@ -2,7 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ClassicalSharp.Map; using ClassicalSharp.Map;
using OpenTK; using ClassicalSharp.Events;
namespace ClassicalSharp.Singleplayer { namespace ClassicalSharp.Singleplayer {
@ -35,6 +35,7 @@ namespace ClassicalSharp.Singleplayer {
map = game.World; map = game.World;
info = game.BlockInfo; info = game.BlockInfo;
game.WorldEvents.OnNewMapLoaded += ResetMap; game.WorldEvents.OnNewMapLoaded += ResetMap;
game.UserEvents.BlockChanged += BlockChanged;
enabled = Options.GetBool( OptionsKey.SingleplayerPhysics, true ); enabled = Options.GetBool( OptionsKey.SingleplayerPhysics, true );
falling = new FallingPhysics( game, this ); falling = new FallingPhysics( game, this );
@ -85,11 +86,12 @@ namespace ClassicalSharp.Singleplayer {
TickRandomBlocks(); TickRandomBlocks();
} }
public void OnBlockPlaced( int x, int y, int z, byte block ) { void BlockChanged( object sender, BlockChangedEventArgs e ) {
if( !Enabled ) return; if( !Enabled || e.Block == 0 ) return;
int index = (y * length + z) * width + x; Vector3I p = e.Coords;
Action<int, byte> place = OnPlace[block]; int index = (p.Y * length + p.Z) * width + p.X;
if( place != null ) place( index, block ); Action<int, byte> place = OnPlace[e.Block];
if( place != null ) place( index, e.Block );
} }
void ResetMap( object sender, EventArgs e ) { void ResetMap( object sender, EventArgs e ) {
@ -108,6 +110,7 @@ namespace ClassicalSharp.Singleplayer {
public void Dispose() { public void Dispose() {
game.WorldEvents.OnNewMapLoaded -= ResetMap; game.WorldEvents.OnNewMapLoaded -= ResetMap;
game.UserEvents.BlockChanged -= BlockChanged;
} }
void TickRandomBlocks() { void TickRandomBlocks() {

View file

@ -61,11 +61,6 @@ namespace ClassicalSharp.Singleplayer {
public override void SendPosition( Vector3 pos, float yaw, float pitch ) { public override void SendPosition( Vector3 pos, float yaw, float pitch ) {
} }
public override void SendSetBlock( int x, int y, int z, bool place, byte block ) {
if( place )
physics.OnBlockPlaced( x, y, z, block );
}
public override void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId, PickedPos pos ) { public override void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId, PickedPos pos ) {
} }