Skip to content

Commit

Permalink
adds simple terrain generation/adjusts delete mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Feb 27, 2021
1 parent 8dca57f commit 79abdad
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 136 deletions.
54 changes: 54 additions & 0 deletions Blocktest/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Unity Editor",
"type": "unity",
"path": "./Library/EditorInstance.json",
"request": "launch"
},
{
"name": "Windows Player",
"type": "unity",
"request": "launch"
},
{
"name": "OSX Player",
"type": "unity",
"request": "launch"
},
{
"name": "Linux Player",
"type": "unity",
"request": "launch"
},
{
"name": "iOS Player",
"type": "unity",
"request": "launch"
},
{
"name": "Android Player",
"type": "unity",
"request": "launch"
},
{
"name": "Xbox One Player",
"type": "unity",
"request": "launch"
},
{
"name": "PS4 Player",
"type": "unity",
"request": "launch"
},
{
"name": "SwitchPlayer",
"type": "unity",
"request": "launch"
}
]
}
85 changes: 85 additions & 0 deletions Blocktest/Assets/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ RectTransform:
m_Children:
- {fileID: 1892915112}
- {fileID: 2088312331}
- {fileID: 1771384453}
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -1096,7 +1097,11 @@ MonoBehaviour:
maxX: 255
maxY: 255
foregroundTilemap: {fileID: 1983780303}
backgroundTilemap: {fileID: 1505424472}
blockManager: {fileID: 1372863295}
stonePercentage: 70
generatorSeed: 0
intensity: 10
--- !u!1 &1505424469
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1369,6 +1374,85 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1672933304}
m_CullTransparentMesh: 1
--- !u!1 &1771384452
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1771384453}
- component: {fileID: 1771384455}
- component: {fileID: 1771384454}
m_Layer: 5
m_Name: Version
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1771384453
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1771384452}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 750836953}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0.5}
m_AnchorMax: {x: 1, y: 0.5}
m_AnchoredPosition: {x: -80, y: 251}
m_SizeDelta: {x: 160, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1771384454
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1771384452}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 14
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 10
m_MaxSize: 40
m_Alignment: 1
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Blocktest alpha-0.0.1
--- !u!222 &1771384455
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1771384452}
m_CullTransparentMesh: 1
--- !u!1 &1877863276
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1732,6 +1816,7 @@ MonoBehaviour:
groundLayer:
serializedVersion: 2
m_Bits: 512
perlinNoise: 0
--- !u!95 &1910289006
Animator:
serializedVersion: 3
Expand Down
2 changes: 1 addition & 1 deletion Blocktest/Assets/Scripts/BlockManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BlockManager : MonoBehaviour {
[SerializeField] Dropdown selectionDropdown;


private void Start()
private void Awake()
{
// Initialise allBlocks array.
allBlocks = new Block[allBlockTypes.Length];
Expand Down
100 changes: 85 additions & 15 deletions Blocktest/Assets/Scripts/BuildSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,23 @@ void Update()
currentRenderer.color = new Color(0.5f, 1f, 0.5f, 0.7f); // Otherwise, normal coloring
}

if (canBuildForeground && Input.GetMouseButton(0))
{
PlaceBlock(currentBlock, true, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
if (canBuildBackground && Input.GetMouseButton(1))
{
PlaceBlock(currentBlock, false, Camera.main.ScreenToWorldPoint(Input.mousePosition));
if (canBuildForeground && Input.GetMouseButton(0)) {
BuildBlock(currentBlock, true, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
else if (canBuildBackground && Input.GetMouseButton(1)) {
BuildBlock(currentBlock, false, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}

}

if(!buildMode && Input.GetMouseButton(0))
if(!buildMode)
{
BreakBlock(Camera.main.ScreenToWorldPoint(Input.mousePosition));
if(Input.GetMouseButton(0)) {
BreakBlock(true, Camera.main.ScreenToWorldPoint(Input.mousePosition));
} else if(Input.GetMouseButton(1)) {
BreakBlock(false, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}

}

}
Expand Down Expand Up @@ -158,28 +161,95 @@ private void InitializeCursor()
}
}

public void BreakBlock(Vector2 position)
public void BreakBlock(bool foreground, Vector2 position)
{
Vector3Int tilePosition = foregroundTilemap.WorldToCell(position);
if(foregroundTilemap.HasTile(tilePosition)) {
if(foreground && foregroundTilemap.HasTile(tilePosition)) {
foregroundTilemap.SetTile(tilePosition, null);
} else if (backgroundTilemap.HasTile(tilePosition)) {
} else if (!foreground && backgroundTilemap.HasTile(tilePosition)) {
backgroundTilemap.SetTile(tilePosition, null);
}
}

public void PlaceBlock(Block toPlace, bool foreground, Vector2 position)
//
// Summary:
// The method called whenever a PLAYER places an object.
// Parameters:
// toPlace:
// The block type to place.
// foreground:
// Whether or not the block should be placed in the foreground.
// position:
// The position of the placed block (world coords)
private void BuildBlock(Block toPlace, bool foreground, Vector2 position)
{
audioSource.PlayOneShot(currentBlock.placeSound);
PlaceBlockWorld(toPlace, foreground, position);
}

//
// Summary:
// The method called whenever a block is placed.
// Parameters:
// toPlace:
// The block type to place.
// foreground:
// Whether or not the block should be placed in the foreground.
// position:
// The position of the placed block
public void PlaceBlockWorld(Block toPlace, bool foreground, Vector2 position)
{
PlaceBlockCell(toPlace, foreground, foregroundTilemap.WorldToCell(position));
}

//
// Summary:
// The method called whenever a block is placed.
// Parameters:
// toPlace:
// The block type to place.
// foreground:
// Whether or not the block should be placed in the foreground.
// position:
// The position of the placed block
public void PlaceBlockCell(Block toPlace, bool foreground, Vector3Int tilePosition)
{
Vector3Int tilePosition = foregroundTilemap.WorldToCell(position);
BlockTile newTile = BlockTile.CreateInstance<BlockTile>();
newTile.sourceBlock = toPlace;
newTile.sprite = toPlace.blockSprite;
newTile.name = toPlace.blockName;
audioSource.PlayOneShot(currentBlock.placeSound);

if(foreground) {
Debug.Log(tilePosition);
newTile.colliderType = Tile.ColliderType.Grid;
foregroundTilemap.SetTile(tilePosition, newTile);
} else {
newTile.color = new Color(0.5f, 0.5f, 0.5f, 1f);
backgroundTilemap.SetTile(tilePosition, newTile);
}
}

//
// Summary:
// The method called whenever a block is placed.
// Parameters:
// toPlace:
// The block type to place.
// foreground:
// Whether or not the block should be placed in the foreground.
// position:
// The position of the placed block
public void PlaceBlockCell(Block toPlace, bool foreground, Vector2 position)
{
Vector3Int tilePosition = new Vector3Int(Mathf.RoundToInt(position.x), Mathf.RoundToInt(position.y), 0);
BlockTile newTile = BlockTile.CreateInstance<BlockTile>();
newTile.sourceBlock = toPlace;
newTile.sprite = toPlace.blockSprite;
newTile.name = toPlace.blockName;

if(foreground) {
Debug.Log(tilePosition);
newTile.colliderType = Tile.ColliderType.Grid;
foregroundTilemap.SetTile(tilePosition, newTile);
} else {
newTile.color = new Color(0.5f, 0.5f, 0.5f, 1f);
Expand Down
56 changes: 44 additions & 12 deletions Blocktest/Assets/Scripts/WorldGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,73 @@

public class WorldGen : MonoBehaviour
{
[SerializeField] int maxX = 255;
[SerializeField] int maxY = 255;
[SerializeField] Tilemap foregroundTilemap;
[SerializeField] Tilemap backgroundTilemap;
[SerializeField] BlockManager blockManager;
[SerializeField] BuildSystem buildSystem;

[SerializeField] int stonePercentage;
/// The type of block used on the lowest layer.

public Block stoneBlock;
/// The type of block used on the middle layer.
public Block dirtBlock;
/// The type of block used on the topmost layer.
public Block grassBlock;
// Start is called before the first frame update

/// The terrain seed used.
public float worldSeed;
/// The "intensity" of the generated area's elevation changes
[SerializeField] float intensity = 10.0f;
///Progress of the generator on the generation

public float progress = 0.0f;

void Start()
{
// TODO: use Mathf.PerlinNoise()
blockManager = GetComponent<BlockManager>();
buildSystem = GetComponent<BuildSystem>();

worldSeed = Random.Range(0.0f, 1000000.0f);
Generate(255, 255, worldSeed);
}

public void Generate(int maxX = 255, int maxY = 255, float generatorSeed = 0.0f)
{
if(generatorSeed == 0.0f) {
generatorSeed = Random.Range(0.0f, 1000000.0f);
}

dirtBlock = blockManager.allBlocks[0]; // TODO: Find a way to dynamically reserve certain blocks
grassBlock = blockManager.allBlocks[1];
stoneBlock = blockManager.allBlocks[2];

progress = 0.0f;

for (int xi = 0; xi < maxX; xi++) {
float x = (float)xi / 10;
float result = Mathf.PerlinNoise(x * intensity + generatorSeed, generatorSeed);
int height = Mathf.RoundToInt(result * maxY); // Gets the height of the column
int stoneHeight = Mathf.RoundToInt((float)height * ((float)stonePercentage / 100));

for (int yi = 0; yi < maxY; yi++) {
Block toPlace;
if(yi < 30) {
if(yi < stoneHeight) {
toPlace = stoneBlock;
}
else if(yi < 40) {
else if(yi < height) {
toPlace = dirtBlock;
}
else if(yi == 40) {
else if(yi == height) {
toPlace = grassBlock;
}
else {
continue;
}

BlockTile newTile = BlockTile.CreateInstance<BlockTile>();
newTile.sourceBlock = toPlace;
newTile.sprite = toPlace.blockSprite;
newTile.name = toPlace.blockName;
foregroundTilemap.SetTile(new Vector3Int(xi, yi, 0), newTile);
buildSystem.PlaceBlockCell(toPlace, true, new Vector2(xi, yi));
buildSystem.PlaceBlockCell(toPlace, false, new Vector2(xi, yi));
//progress += 1.0f / ((float)maxX + (float)maxY); // Add progress to the progress var
}
}
}
Expand Down
Binary file removed Blocktest/Assets/Sprites/delete.gif
Binary file not shown.
Loading

0 comments on commit 79abdad

Please sign in to comment.