Separate inventory handling.

This commit is contained in:
UnknownShadow200 2015-06-27 16:42:54 +10:00
parent 0057e6d9f3
commit 589a9e0beb
10 changed files with 173 additions and 73 deletions

View file

@ -23,6 +23,7 @@ namespace ClassicalSharp {
}
BlockDrawInfo[] blocksTable;
Texture2D selectedBlock, blockInfoTexture;
Inventory inventory;
int blockSize = 48;
int selectedIndex = 0;
const int blocksPerRow = 10;
@ -73,6 +74,7 @@ namespace ClassicalSharp {
}
public override void Init() {
inventory = Window.Inventory;
Window.BlockPermissionsChanged += BlockPermissionsChanged;
Size size = new Size( blockSize, blockSize );
using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) {
@ -112,9 +114,9 @@ namespace ClassicalSharp {
}
}
buffer.Append( ref ptr2, " (can place: " );
buffer.Append( ref ptr2, Window.CanPlace[(int)block] ? "&aYes" : "&cNo" );
buffer.Append( ref ptr2, inventory.CanPlace[(int)block] ? "&aYes" : "&cNo" );
buffer.Append( ref ptr2, "&f, can delete: " );
buffer.Append( ref ptr2, Window.CanDelete[(int)block] ? "&aYes" : "&cNo" );
buffer.Append( ref ptr2, inventory.CanDelete[(int)block] ? "&aYes" : "&cNo" );
buffer.Append( ref ptr2, "&f)" );
}
}
@ -146,7 +148,7 @@ namespace ClassicalSharp {
void RecreateBlockTextures() {
int blocksCount = 0;
for( int i = 0; i < Window.BlockInfo.BlocksCount; i++ ) {
if( Window.CanPlace[i] || Window.CanDelete[i] ) {
if( inventory.CanPlace[i] || inventory.CanDelete[i] ) {
blocksCount++;
}
}
@ -159,7 +161,7 @@ namespace ClassicalSharp {
int tableIndex = 0;
for( int tile = 1; tile < Window.BlockInfo.BlocksCount; tile++ ) {
if( Window.CanPlace[tile] || Window.CanDelete[tile] ) {
if( inventory.CanPlace[tile] || inventory.CanDelete[tile] ) {
Block block = (Block)tile;
int texId = Window.BlockInfo.GetOptimTextureLoc( (byte)block, TileSide.Left );
TextureRectangle rec = Window.TerrainAtlas.GetTexRec( texId );
@ -209,7 +211,7 @@ namespace ClassicalSharp {
public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) {
if( button == MouseButton.Left && selectedIndex != -1 ) {
BlockDrawInfo info = blocksTable[selectedIndex];
Window.HeldBlock = info.BlockId;
inventory.HeldBlock = info.BlockId;
Window.SetNewScreen( new NormalScreen( Window ) );
}
return true;

View file

@ -15,11 +15,12 @@ namespace ClassicalSharp {
Texture2D[] barTextures = new Texture2D[9];
Texture2D selectedBlock;
Inventory inventory;
const int blockSize = 32;
public override bool HandlesKeyDown( Key key ) {
if( key >= Key.Number1 && key <= Key.Number9 ) {
Window.HeldBlockIndex = (int)key - (int)Key.Number1;
inventory.HeldBlockIndex = (int)key - (int)Key.Number1;
return true;
}
return false;
@ -27,6 +28,7 @@ namespace ClassicalSharp {
public override void Init() {
int y = Window.Height - blockSize;
inventory = Window.Inventory;
Size size = new Size( 32, 32 );
using( Bitmap bmp = Utils2D.CreatePow2Bitmap( size ) ) {
@ -43,7 +45,7 @@ namespace ClassicalSharp {
Height = blockSize;
for( int i = 0; i < barTextures.Length; i++ ) {
barTextures[i] = MakeTexture( x, y, Window.BlocksHotbar[i] );
barTextures[i] = MakeTexture( x, y, inventory.BlocksHotbar[i] );
x += blockSize;
}
}
@ -54,7 +56,7 @@ namespace ClassicalSharp {
int selectedX = 0;
for( int i = 0; i < barTextures.Length; i++ ) {
barTextures[i].RenderNoBind( GraphicsApi );
if( i == Window.HeldBlockIndex ) {
if( i == inventory.HeldBlockIndex ) {
selectedX = barTextures[i].X1;
}
}
@ -68,8 +70,8 @@ namespace ClassicalSharp {
}
void HeldBlockChanged( object sender, EventArgs e ) {
int index = Window.HeldBlockIndex;
Block block = Window.HeldBlock;
int index = inventory.HeldBlockIndex;
Block block = inventory.HeldBlock;
int x = barTextures[index].X1;
barTextures[index] = MakeTexture( x, Y, block );
}

View file

@ -120,6 +120,7 @@
<Compile Include="Game\Game.cs" />
<Compile Include="Game\Game.Events.cs" />
<Compile Include="Game\Game.InputHandling.cs" />
<Compile Include="Game\Inventory.cs" />
<Compile Include="GraphicsAPI\DefaultShaders.cs" />
<Compile Include="GraphicsAPI\IGraphicsApi.cs" />
<Compile Include="GraphicsAPI\ModernGLApi.cs" />

View file

@ -0,0 +1,75 @@
using System;
namespace ClassicalSharp {
/// <summary> Enumeration of all blocks in Minecraft Classic, including CPE ones. </summary>
public enum Block : byte {
Air = 0,
Stone = 1,
Grass = 2,
Dirt = 3,
Cobblestone = 4,
WoodenPlanks = 5,
Sapling = 6,
Bedrock = 7,
Water = 8,
StillWater = 9,
Lava = 10,
StillLava = 11,
Sand = 12,
Gravel = 13,
GoldOre = 14,
IronOre = 15,
CoalOre = 16,
Wood = 17,
Leaves = 18,
Sponge = 19,
Glass = 20,
RedCloth = 21,
OrangeCloth = 22,
YellowCloth = 23,
LimeCloth = 24,
GreenCloth = 25,
AquaCloth = 26,
CyanCloth = 27,
BlueCloth = 28,
PurpleCloth = 29,
IndigoCloth = 30,
VioletCloth = 31,
MagentaCloth = 32,
PinkCloth = 33,
BlackCloth = 34,
GrayCloth = 35,
WhiteCloth = 36,
Dandelion = 37,
Rose = 38,
BrownMushroom = 39,
RedMushroom = 40,
GoldBlock = 41,
IronBlock = 42,
DoubleSlab = 43,
Slab = 44,
Brick = 45,
TNT = 46,
Bookshelf = 47,
MossyCobblestone = 48,
Obsidian = 49,
CobblestoneSlab = 50,
Rope = 51,
Sandstone = 52,
Snow = 53,
Fire = 54,
LightPinkWool = 55,
ForestGreenWool = 56,
BrownWool = 57,
DeepBlueWool = 58,
TurquoiseWool = 59,
Ice = 60,
CeramicTile = 61,
Magma = 62,
Pillar = 63,
Crate = 64,
StoneBrick = 65,
}
}

View file

@ -42,7 +42,7 @@ namespace DefaultPlugin.Network {
ServerMotd = reader.ReadString();
byte userType = reader.ReadUInt8();
if( !useBlockPermissions ) {
Window.CanDelete[(int)Block.Bedrock] = userType == 0x64;
Window.Inventory.CanDelete[(int)Block.Bedrock] = userType == 0x64;
}
Window.LocalPlayer.UserType = userType;
receivedFirstPosition = false;
@ -177,7 +177,7 @@ namespace DefaultPlugin.Network {
{
byte userType = reader.ReadUInt8();
if( !useBlockPermissions ) {
Window.CanDelete[(int)Block.Bedrock] = userType == 0x64;
Window.Inventory.CanDelete[(int)Block.Bedrock] = userType == 0x64;
}
Window.LocalPlayer.UserType = userType;
} break;
@ -226,8 +226,8 @@ namespace DefaultPlugin.Network {
if( supportLevel == 1 ) {
for( int i = (int)Block.CobblestoneSlab; i <= (int)Block.StoneBrick; i++ ) {
Window.CanPlace[i] = true;
Window.CanDelete[i] = true;
Window.Inventory.CanPlace[i] = true;
Window.Inventory.CanDelete[i] = true;
}
Window.RaiseBlockPermissionsChanged();
} else {
@ -240,9 +240,9 @@ namespace DefaultPlugin.Network {
{
byte blockType = reader.ReadUInt8();
bool noChanging = reader.ReadUInt8() != 0;
Window.CanChangeHeldBlock = true;
Window.HeldBlock = (Block)blockType;
Window.CanChangeHeldBlock = !noChanging;
Window.Inventory.CanChangeHeldBlock = true;
Window.Inventory.HeldBlock = (Block)blockType;
Window.Inventory.CanChangeHeldBlock = !noChanging;
} break;
case PacketId.CpeExtAddPlayerName:
@ -337,13 +337,14 @@ namespace DefaultPlugin.Network {
bool canPlace = reader.ReadUInt8() != 0;
bool canDelete = reader.ReadUInt8() != 0;
if( blockId == 0 ) {
for( int i = 1; i < Window.CanPlace.Length; i++ ) {
Window.CanPlace[i] = canPlace;
Window.CanDelete[i] = canDelete;
int blocksCount = Window.BlockInfo.BlocksCount;
for( int i = 1; i < blocksCount; i++ ) {
Window.Inventory.CanPlace[i] = canPlace;
Window.Inventory.CanDelete[i] = canDelete;
}
} else {
Window.CanPlace[blockId] = canPlace;
Window.CanDelete[blockId] = canDelete;
Window.Inventory.CanPlace[blockId] = canPlace;
Window.Inventory.CanDelete[blockId] = canDelete;
}
Window.RaiseBlockPermissionsChanged();
} break;

View file

@ -32,7 +32,7 @@ namespace DefaultPlugin.Network {
}
public override void SendPosition( Vector3 pos, byte yaw, byte pitch ) {
byte payload = sendHeldBlock ? (byte)Window.HeldBlock : (byte)0xFF;
byte payload = sendHeldBlock ? (byte)Window.Inventory.HeldBlock : (byte)0xFF;
WritePacket( MakePositionPacket( pos, yaw, pitch, payload ) );
}

View file

@ -54,12 +54,8 @@ namespace ClassicalSharp {
}
void MouseWheelChanged( object sender, MouseWheelEventArgs e ) {
if( !Camera.MouseZoom( e ) && CanChangeHeldBlock ) {
int diffIndex = -e.Delta % BlocksHotbar.Length;
int newIndex = HeldBlockIndex + diffIndex;
if( newIndex < 0 ) newIndex += BlocksHotbar.Length;
if( newIndex >= BlocksHotbar.Length ) newIndex -= BlocksHotbar.Length;
HeldBlockIndex = newIndex;
if( !Camera.MouseZoom( e ) && Inventory.CanChangeHeldBlock ) {
Inventory.ScrollHeldIndex( -e.Delta );
}
}
@ -123,7 +119,7 @@ namespace ClassicalSharp {
DateTime lastClick = DateTime.MinValue;
void PickBlocks( bool cooldown, bool left, bool right, bool middle ) {
int buttonsDown = ( left ? 1 : 0 ) + ( right ? 1 : 0 ) + ( middle ? 1 : 0 );
if( !SelectedPos.Valid || buttonsDown > 1 || ScreenLockedInput || HeldBlock == Block.Air ) return;
if( !SelectedPos.Valid || buttonsDown > 1 || ScreenLockedInput || Inventory.HeldBlock == Block.Air ) return;
DateTime now = DateTime.UtcNow;
double delta = ( now - lastClick ).TotalMilliseconds;
if( cooldown && delta < 250 ) return; // 4 times per second
@ -132,13 +128,13 @@ namespace ClassicalSharp {
if( middle ) {
Vector3I pos = SelectedPos.BlockPos;
byte block = Map.GetBlock( pos );
if( block != 0 && ( CanPlace[block] || CanDelete[block] ) ) {
HeldBlock = (Block)block;
if( block != 0 && ( Inventory.CanPlace[block] || Inventory.CanDelete[block] ) ) {
Inventory.HeldBlock = (Block)block;
}
} else if( left ) {
Vector3I pos = SelectedPos.BlockPos;
byte block = Map.GetBlock( pos );
if( block != 0 && CanDelete[block] ) {
if( block != 0 && Inventory.CanDelete[block] ) {
ParticleManager.BreakBlockEffect( pos, block );
Network.SendSetBlock( pos.X, pos.Y, pos.Z, 0 );
UpdateBlock( pos.X, pos.Y, pos.Z, 0 );
@ -147,18 +143,14 @@ namespace ClassicalSharp {
Vector3I pos = SelectedPos.TranslatedPos;
if( !Map.IsValidPos( pos ) ) return;
Block block = HeldBlock;
if( CanReplace( Map.GetBlock( pos ) ) && CanPlace[(int)block] ) {
Block block = Inventory.HeldBlock;
if( Inventory.CanReplace( Map.GetBlock( pos ) ) && Inventory.CanPlace[(int)block] ) {
Network.SendSetBlock( pos.X, pos.Y, pos.Z, (byte)block );
UpdateBlock( pos.X, pos.Y, pos.Z, (byte)block );
}
}
}
bool CanReplace( byte block ) {
return block == 0 || ( BlockInfo.IsLiquid( block ) && !CanPlace[block] && !CanDelete[block] );
}
public KeyMap Keys = new KeyMap();
}

View file

@ -21,6 +21,7 @@ namespace ClassicalSharp {
public OpenGLApi Graphics;
public Map Map;
public NetworkProcessor Network;
public Inventory Inventory;
public List<Type> NetworkProcessorTypes = new List<Type>();
public Player[] NetPlayers = new Player[256];
@ -54,38 +55,6 @@ namespace ClassicalSharp {
public bool CanUseThirdPersonCamera = true;
FpsScreen fpsScreen;
int hotbarIndex = 0;
public bool CanChangeHeldBlock = true;
public Block[] BlocksHotbar = new Block[] { Block.Stone, Block.Cobblestone,
Block.Brick, Block.Dirt, Block.WoodenPlanks, Block.Wood, Block.Leaves, Block.Glass, Block.Slab };
public int HeldBlockIndex {
get { return hotbarIndex; }
set {
if( !CanChangeHeldBlock ) {
AddChat( "&e/client: &cThe server has forbidden you from changing your held block." );
return;
}
hotbarIndex = value;
RaiseHeldBlockChanged();
}
}
public Block HeldBlock {
get { return BlocksHotbar[hotbarIndex]; }
set {
if( !CanChangeHeldBlock ) {
AddChat( "&e/client: &cThe server has forbidden you from changing your held block." );
return;
}
BlocksHotbar[hotbarIndex] = value;
RaiseHeldBlockChanged();
}
}
public bool[] CanPlace = new bool[256];
public bool[] CanDelete = new bool[256];
public IPAddress IPAddress;
public string Username;
public string Mppass;
@ -130,6 +99,7 @@ namespace ClassicalSharp {
LoadAtlas( terrainBmp );
VSync = VSyncMode.On;
Inventory = new Inventory( this );
Graphics.DepthTest = true;
Graphics.DepthTestFunc( CompareFunc.LessEqual );
//Graphics.DepthWrite = true;
@ -164,7 +134,7 @@ namespace ClassicalSharp {
BlockInfo = Utils.New<BlockInfo>( BlockInfoTypes[0], this );
BlockInfo.Init();
BlockInfo.SetDefaultBlockPermissions( CanPlace, CanDelete );
BlockInfo.SetDefaultBlockPermissions( Inventory.CanPlace, Inventory.CanDelete );
Map = new Map( this );
LocalPlayer = new LocalPlayer( 255, this );
SelectionManager = new SelectionManager( this );

57
Game/Inventory.cs Normal file
View file

@ -0,0 +1,57 @@
using System;
namespace ClassicalSharp {
public class Inventory {
public Game Window;
public Inventory( Game window ) {
Window = window;
}
int hotbarIndex = 0;
public bool CanChangeHeldBlock = true;
public Block[] BlocksHotbar = new Block[] { Block.Stone, Block.Cobblestone,
Block.Brick, Block.Dirt, Block.WoodenPlanks, Block.Wood, Block.Leaves, Block.Glass, Block.Slab };
public int HeldBlockIndex {
get { return hotbarIndex; }
set {
if( !CanChangeHeldBlock ) {
Window.AddChat( "&e/client: &cThe server has forbidden you from changing your held block." );
return;
}
hotbarIndex = value;
Window.RaiseHeldBlockChanged();
}
}
public Block HeldBlock {
get { return BlocksHotbar[hotbarIndex]; }
set {
if( !CanChangeHeldBlock ) {
Window.AddChat( "&e/client: &cThe server has forbidden you from changing your held block." );
return;
}
BlocksHotbar[hotbarIndex] = value;
Window.RaiseHeldBlockChanged();
}
}
public bool CanReplace( byte block ) {
return block == 0 || ( Window.BlockInfo.IsLiquid( block ) && !CanPlace[block] && !CanDelete[block] );
}
public bool[] CanPlace = new bool[256];
public bool[] CanDelete = new bool[256];
internal void ScrollHeldIndex( int diff ) {
int diffIndex = diff % BlocksHotbar.Length;
int newIndex = HeldBlockIndex + diffIndex;
if( newIndex < 0 ) newIndex += BlocksHotbar.Length;
if( newIndex >= BlocksHotbar.Length ) newIndex -= BlocksHotbar.Length;
HeldBlockIndex = newIndex;
}
}
}

View file

@ -71,7 +71,7 @@ namespace ClassicalSharp {
byte block;
if( map.IsValidPos( x, y, z ) && ( block = map.GetBlock( x, y, z ) ) != 0 ) {
bool cantPickBlock = !window.CanPlace[block] && !window.CanDelete[block] && info.IsLiquid( block );
bool cantPickBlock = !window.Inventory.CanPlace[block] && !window.Inventory.CanDelete[block] && info.IsLiquid( block );
if( !cantPickBlock ) {
// This cell falls on the path of the ray. Now perform an additional bounding box test,
// since some blocks do not occupy a whole cell.