Style: move autorotate stuff into separate file

This commit is contained in:
UnknownShadow200 2016-12-07 21:55:00 +11:00
parent b785c3e2b7
commit e769fe8b7f
3 changed files with 88 additions and 123 deletions

View File

@ -0,0 +1,84 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using ClassicalSharp.Blocks;
using OpenTK;
namespace ClassicalSharp {
/// <summary> Performs automatic rotation of directional blocks. </summary>
public static class AutoRotate {
public static byte RotateBlock(Game game, byte block) {
string name = game.BlockInfo.Name[block];
int sepIndex = name.LastIndexOf('-');
if (sepIndex == -1) return block; // not a directional block
string sep = name.Substring(sepIndex + 1);
name = name.Substring(0, sepIndex);
Vector3 offset = game.SelectedPos.Intersect - (Vector3)game.SelectedPos.TranslatedPos;
if (Utils.CaselessEquals(sep, "nw") || Utils.CaselessEquals(sep, "ne") ||
Utils.CaselessEquals(sep, "sw") || Utils.CaselessEquals(sep, "se")) {
return RotateCorner(game, block, name, offset);
} else if (Utils.CaselessEquals(sep, "u") || Utils.CaselessEquals(sep, "d")) {
return RotateVertical(game, block, name, offset);
} else if (Utils.CaselessEquals(sep, "n") || Utils.CaselessEquals(sep, "w") ||
Utils.CaselessEquals(sep, "s") || Utils.CaselessEquals(sep, "e")) {
return RotateDirection(game, block, name, offset);
}
return block;
}
static byte RotateCorner(Game game, byte block, string name, Vector3 offset) {
if (offset.X < 0.5f && offset.Z < 0.5f) {
return Find(game, block, name + "-NW");
} else if (offset.X >= 0.5f && offset.Z < 0.5f) {
return Find(game, block, name + "-NE");
} else if (offset.X < 0.5f && offset.Z >= 0.5f) {
return Find(game, block, name + "-SW");
} else if (offset.X >= 0.5f && offset.Z >= 0.5f) {
return Find(game, block, name + "-SE");
}
return block;
}
static byte RotateVertical(Game game, byte block, string name, Vector3 offset) {
string height = offset.Y >= 0.5f ? "-U" : "-D";
return Find(game, block, name + height);
}
static byte RotateDirection(Game game, byte block, string name, Vector3 offset) {
Vector3 southEast = new Vector3 (1,0,1);
Vector3 southWest = new Vector3 (-1, 0, 1);
Vector3I pos = game.SelectedPos.TranslatedPos;
Vector3 posExact = game.SelectedPos.Intersect;
Vector3 posExactFlat = posExact; posExactFlat.Y = 0;
Vector3 southEastToPoint = posExactFlat - new Vector3 (pos.X, 0, pos.Z);
Vector3 southWestToPoint = posExactFlat - new Vector3 (pos.X +1, 0, pos.Z);
float dotSouthEast = Vector3.Dot(southEastToPoint, southWest);
float dotSouthWest= Vector3.Dot(southWestToPoint, southEast);
if (dotSouthEast <= 0) { // NorthEast
if (dotSouthWest <= 0) { //NorthWest
return Find(game, block, name + "-N");
} else { //SouthEast
return Find(game, block, name + "-E");
}
} else { //SouthWest
if (dotSouthWest <= 0) { //NorthWest
return Find(game, block, name + "-W");
} else { //SouthEast
return Find(game, block, name + "-S");
}
}
}
static byte Find(Game game, byte block, string name) {
int rotated = game.BlockInfo.FindID(name);
if (rotated != -1) return (byte)rotated;
return block;
}
}
}

View File

@ -142,6 +142,7 @@
<Compile Include="Audio\AudioPlayer.cs" />
<Compile Include="Audio\AudioPlayer.Sounds.cs" />
<Compile Include="Audio\Soundboard.cs" />
<Compile Include="Blocks\AutoRotate.cs" />
<Compile Include="Blocks\Block.cs" />
<Compile Include="Blocks\BlockInfo.BoundsBox.cs" />
<Compile Include="Blocks\BlockInfo.cs" />

View File

@ -64,133 +64,13 @@ namespace ClassicalSharp {
game.UserEvents.RaiseBlockChanged(pos, old, 0);
}
} else if (right) {
//[1:28:03 PM] <UnknownShadow200+> well goodlyay you have game.SelectedPos.Intersect
//[1:28:17 PM] <UnknownShadow200+> that's the exact floating point coordinates that the mouse clicked on the block
//[1:31:45 PM] <UnknownShadow200+> e.g. the block clicked on might be (32, 34, 63)
//[1:31:55 PM] <UnknownShadow200+> Intersect might be (33, 34.51049, 63.44751)
Vector3I pos = game.SelectedPos.TranslatedPos;
Vector3 posExact = game.SelectedPos.Intersect;
if (!game.World.IsValidPos(pos)) return;
byte old = game.World.GetBlock(pos);
byte block = (byte)inv.HeldBlock;
if (game.autoRotate) {
string[] blockNameSplit = info.Name[block].ToUpper().Split('-');
bool isDirectional = false;
bool isHeight = false;
bool isCorner = false;
if (blockNameSplit.Length == 2) {
switch (blockNameSplit[1]) {
case "N": case "E": case "W": case "S":
isDirectional = true;
break;
}
switch (blockNameSplit[1]) {
case "U": case "D":
isHeight = true;
break;
}
switch (blockNameSplit[1]) {
//NW NE
//SW SE
case "NW": case "NE": case "SW": case "SE":
isCorner = true;
break;
}
}
if (isDirectional) {
Vector3 southEast = new Vector3 (1,0,1);
Vector3 southWest = new Vector3 (-1, 0, 1);
Vector3 posExactFlat = posExact; posExactFlat.Y = 0;
Vector3 southEastToPoint = posExactFlat - new Vector3 (pos.X, 0, pos.Z);
Vector3 southWestToPoint = posExactFlat - new Vector3 (pos.X +1, 0, pos.Z);
float dotSouthEast = Vector3.Dot(southEastToPoint, southWest);
float dotSouthWest= Vector3.Dot(southWestToPoint, southEast);
string direction;
if (dotSouthEast <= 0) { // NorthEast
if (dotSouthWest <= 0) { //NorthWest
//North
direction = "N";
} else { //SouthEast
//East
direction = "E";
}
} else { //SouthWest
if (dotSouthWest <= 0) { //NorthWest
//West
direction = "W";
} else { //SouthEast
//South
direction = "S";
}
}
if (direction != blockNameSplit[1]) {
string newBlockName = blockNameSplit[0] + "-" + direction;
int newBlockID = game.BlockInfo.FindID(newBlockName);
if (newBlockID != -1) {
block = (byte)newBlockID;
//game.Chat.Add("Substituted " + block + " for " + newBlockID + ".");
} else {
//game.Chat.Add("could not find " + newBlockName + ".");
}
}
}
if (isHeight) {
string height = "D";
float pickedHeight = posExact.Y - pos.Y;
//game.Chat.Add("pickedHeight: " + pickedHeight + ".");
if (pickedHeight >= 0.5f) {
height = "U";
}
if (height != blockNameSplit[1]) {
string newBlockName = blockNameSplit[0] + "-" + height;
int newBlockID = game.BlockInfo.FindID(newBlockName);
if (newBlockID != -1) {
block = (byte)newBlockID;
//game.Chat.Add("Substituted " + block + " for " + newBlockID + ".");
} else {
//game.Chat.Add("could not find " + newBlockName + ".");
}
}
}
if (isCorner) {
string corner = "NW";
Vector3 pickedCorner = posExact - (Vector3)pos;
//game.Chat.Add("pickedHeight: " + pickedHeight + ".");
//NW NE
//SW SE
if (pickedCorner.X < 0.5f && pickedCorner.Z < 0.5f) {
corner = "NW";
}
if (pickedCorner.X >= 0.5f && pickedCorner.Z < 0.5f) {
corner = "NE";
}
if (pickedCorner.X < 0.5f && pickedCorner.Z >= 0.5f) {
corner = "SW";
}
if (pickedCorner.X >= 0.5f && pickedCorner.Z >= 0.5f) {
corner = "SE";
}
if (corner != blockNameSplit[1]) {
string newBlockName = blockNameSplit[0] + "-" + corner;
int newBlockID = game.BlockInfo.FindID(newBlockName);
if (newBlockID != -1) {
block = (byte)newBlockID;
//game.Chat.Add("Substituted " + block + " for " + newBlockID + ".");
} else {
//game.Chat.Add("could not find " + newBlockName + ".");
}
}
}
}
if (game.autoRotate)
block = AutoRotate.RotateBlock(game, block);
if (!game.CanPick(old) && inv.CanPlace[block] && CheckIsFree(game.SelectedPos, block)) {
game.UpdateBlock(pos.X, pos.Y, pos.Z, block);