Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit ca56145

Browse files
committed
Flowers
Raycasted block placement - bad, don't use
1 parent 28b8b62 commit ca56145

File tree

9 files changed

+202
-42
lines changed

9 files changed

+202
-42
lines changed

BlockOutline.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class BlockOutline
2727
public float lineWidth;
2828
public float blocksFromCam;
2929

30+
public bool Raycast = false;
31+
public RaycastResult raycastResult;
32+
3033
public BlockOutline(float size, float _lineWidth, float _blocksFromCam, Color _c)
3134
{
3235
Vector4 c = new Vector4(
@@ -82,12 +85,16 @@ public void UploadMesh()
8285
public void Update()
8386
{
8487
Vector3 dir = Camera.target - Camera.position; // is normalized
85-
Position = (Vector3i)((Camera.position + dir * blocksFromCam) + Vector3.One / 2f);
88+
if (Raycast)
89+
raycastResult = World.Raycast(Camera.position, dir, 0.01f, 20f);
90+
else
91+
raycastResult = new RaycastResult((Vector3i)((Camera.position + dir * blocksFromCam) + Vector3.One / 2f));
92+
Position = raycastResult.HitPos;
8693
}
8794

8895
public void Render(Shader s)
8996
{
90-
if (!Active)
97+
if (!Active) //|| Raycast)
9198
return;
9299

93100
Matrix4 transform = Matrix4.CreateTranslation(Position);

BuildPlate_Editor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="Palette.cs" />
9898
<Compile Include="Program.cs" />
9999
<Compile Include="Properties\AssemblyInfo.cs" />
100+
<Compile Include="RaycastResult.cs" />
100101
<Compile Include="Shader.cs" />
101102
<Compile Include="SkyBox.cs" />
102103
<Compile Include="SubChunk.cs" />

Maths/Vector3i.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public struct Vector3i
1313
{
1414
public static readonly Vector3i Zero = new Vector3i(0, 0, 0);
1515
public static readonly Vector3i One = new Vector3i(1, 1, 1);
16+
public static readonly Vector3i Invalid = new Vector3i(int.MaxValue, int.MaxValue, int.MaxValue);
1617
public static readonly Vector3i UnitX = new Vector3i(1, 0, 0);
1718
public static readonly Vector3i UnitY = new Vector3i(0, 1, 0);
1819
public static readonly Vector3i UnitZ = new Vector3i(0, 0, 1);

Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void Main(string[] args)
9696
texturesPath += '/';
9797
World.textureBasePath = texturesPath;
9898
#if DEBUG
99-
World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\411398a6-5810-43a0-824c-27a9077a9ac3.json";
99+
World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\buildplate (1).json";
100100
//World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\411398a6-5810-43a0-824c-27a9077a9ac3.json"; // fancy
101101
//World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\c0eb3037-94c1-4a85-b0d7-7b8375c6f1b1.json"; // redstone
102102
//World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\c0f771f0-a5d6-4acc-bd35-055e52548a20.json"; // igloo super fancy secret base

RaycastResult.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using BuildPlate_Editor.Maths;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace BuildPlate_Editor
9+
{
10+
public struct RaycastResult
11+
{
12+
public Vector3i HitPos;
13+
public Vector3i LastPos;
14+
15+
public RaycastResult(Vector3i hitPos, Vector3i lastPos)
16+
{
17+
HitPos = hitPos;
18+
LastPos = lastPos;
19+
}
20+
21+
public RaycastResult(Vector3i hitPos) : this(hitPos, hitPos)
22+
{ }
23+
}
24+
}

SubChunk.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ public int GetRenderer(Vector3i pos)
204204

205205
public void GetBlockIndex(int bx, int by, int bz, out int blockIndex)
206206
{
207+
Vector3i offset = new Vector3i(pos.X * VoxelData.ChunkWidth, pos.Y * VoxelData.ChunkHeight, pos.Z * VoxelData.ChunkWidth);
208+
207209
Vector3i block = new Vector3i(bx, by, bz);
208210

209-
Vector3i offset = new Vector3i(pos.X * VoxelData.ChunkWidth, pos.Y * VoxelData.ChunkHeight, pos.Z * VoxelData.ChunkWidth);
211+
block -= offset;
210212

211213
for (int currentBlock = 0; currentBlock < blocks.Length; currentBlock++) {
212-
if (possitionLookUp[currentBlock] + offset == block) {
214+
if (possitionLookUp[currentBlock] == block) {
213215
blockIndex = currentBlock;
214216
return;
215217
}
@@ -348,7 +350,7 @@ public ChunkOutline(Vector3i pos, float size, float _lineWidth, Color _c)
348350

349351
lineWidth = _lineWidth;
350352

351-
Position = pos * size - new Vector3(0.5f, 0.5f, 0.5f);
353+
Position = pos * size + new Vector3(0.5f, 0.5f, 0.5f);
352354

353355
InitMesh();
354356

UI/GUI.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,14 @@ public static void Init(Font font)
9393
UIImage.CreatePixel(new Vector2i(240, 260), new Vector2i(0, 40), Textures["Green"]), // loading bar
9494
UItext.CreateCenter("Loading...", 0, 50, 5f, font),
9595
},
96-
new List<IGUIElement>() { }, // in game
96+
new List<IGUIElement>() {
97+
UIImage.CreateCenter(0.06f, 0.06f, Textures["Crosshair"], true),
98+
}, // in game
9799
new List<IGUIElement>() { }, // save
100+
new List<IGUIElement>() {
101+
new UIImage(-1f, -1f, 2f, 2f, Textures["BlackTransparent"], false),
102+
UItext.CreateCenter("Input to Console", 0, 350, 3f, font),
103+
}, // select block in console
98104
};
99105
AddToScenes(font);
100106
}

Window.cs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
177177
Camera.position.Y += delta * 6f;
178178
else if (keyboardState.IsKeyDown(Key.ShiftLeft))
179179
Camera.position.Y -= delta * 6f;
180+
181+
Camera.UpdateView(Width, Height);
180182
}
181183

182184
if (GUI.Scene == 1 && wantToSave) {
@@ -202,7 +204,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
202204
if (keyboardState.IsKeyDown(Key.Escape))
203205
UnlockMouse();
204206

205-
outline.Update();
207+
//if (!outline.Raycast)
208+
outline.Update();
206209

207210
GUI.Update(delta);
208211

@@ -220,7 +223,6 @@ protected override void OnRenderFrame(FrameEventArgs e)
220223
colShader.UploadMat4("uView", ref Camera.viewMatrix);
221224

222225
shader.Bind();
223-
Camera.UpdateView(Width, Height);
224226
shader.UploadMat4("uProjection", ref Camera.projMatrix);
225227
shader.UploadMat4("uView", ref Camera.viewMatrix);
226228
if (GUI.Scene > 0)
@@ -244,9 +246,30 @@ protected override void OnRenderFrame(FrameEventArgs e)
244246
GL.DepthMask(true);
245247

246248
SwapBuffers();
249+
250+
LateUpdate();
251+
}
252+
253+
private void LateUpdate()
254+
{
255+
if (wantToTakeInput) {
256+
string input = BlockToPlace.TakeInput();
257+
if (input != string.Empty) {
258+
if (!World.WillCreateValidTextures(input, 0)) {
259+
Console.ForegroundColor = ConsoleColor.Red;
260+
Console.WriteLine($"\"{input}\" isn't valid block");
261+
Console.ResetColor();
262+
}
263+
else
264+
World.BlockToPlace = input;
265+
}
266+
GUI.SetScene(0);
267+
wantToTakeInput = false;
268+
}
247269
}
248270

249271
bool wantToSave = false;
272+
bool wantToTakeInput = false;
250273
protected override void OnKeyDown(KeyboardKeyEventArgs e)
251274
{
252275
if (e.Key == Key.P) {
@@ -259,23 +282,15 @@ protected override void OnKeyDown(KeyboardKeyEventArgs e)
259282
Console.WriteLine("Couldn't get block index or sub chunk");
260283
}
261284
else if (e.Key == Key.E) {
262-
string input = BlockToPlace.TakeInput();
263-
if (input != string.Empty) {
264-
if (!World.WillCreateValidTextures(input, 0)) {
265-
Console.ForegroundColor = ConsoleColor.Red;
266-
Console.WriteLine($"\"{input}\" isn't valid block");
267-
Console.ResetColor();
268-
}
269-
else
270-
World.BlockToPlace = input;
271-
}
285+
GUI.SetScene(3);
286+
wantToTakeInput = true;
272287
}
273288
else if (e.Key == Key.S && (e.Modifiers & KeyModifiers.Control) == KeyModifiers.Control) // save
274-
{
275289
wantToSave = true;
276-
}
277290
else if (e.Key == Key.C)
278291
World.ShowChunkOutlines = !World.ShowChunkOutlines;
292+
else if (e.Key == Key.M)
293+
outline.Raycast = !outline.Raycast;
279294
else
280295
keyboardState = e.Keyboard;
281296

@@ -297,10 +312,12 @@ protected override void OnMouseDown(MouseButtonEventArgs e)
297312
GUI.OnMouseDown(e.Button, e.Position);
298313

299314
if (GUI.Scene == 1) {
315+
if (outline.Raycast)
316+
outline.Update();
300317
if (e.Button == MouseButton.Left)
301-
World.SetBlock((Vector3i)outline.Position, "air");
318+
World.SetBlock((Vector3i)outline.raycastResult.HitPos, "air");
302319
if (e.Button == MouseButton.Right)
303-
World.SetBlock((Vector3i)outline.Position, World.BlockToPlace);
320+
World.SetBlock((Vector3i)outline.raycastResult.LastPos, World.BlockToPlace);
304321
}
305322
}
306323
protected override void OnMouseUp(MouseButtonEventArgs e)

0 commit comments

Comments
 (0)