commit no. 5
This commit is contained in:
+36
-8
@@ -1,36 +1,64 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public partial class GridMap2D : Node2D
|
||||
{
|
||||
[Export]
|
||||
public bool _showMarkers = true;
|
||||
public int _cellSize = 16;
|
||||
public Vector2 _sizeInPixels, _sizeInCells;
|
||||
public Area2D _playArea;
|
||||
public GridMarker _gridMarker;
|
||||
public List<StaticBody2D> _leftEdges = new(), _rightEdges = new();
|
||||
public List<List<GridMarker>> _gridMarkers = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
_playArea = GetNode<Area2D>("PlayArea");
|
||||
|
||||
StaticBody2D firstOnLeft = GetNode<StaticBody2D>("LeftEdge1");
|
||||
StaticBody2D firstOnRight = GetNode<StaticBody2D>("RightEdge1");
|
||||
_leftEdges.Add(firstOnLeft);
|
||||
_rightEdges.Add(firstOnRight);
|
||||
CollisionShape2D firstOnLeftBounds = firstOnLeft.GetNode<CollisionShape2D>("Bounds");
|
||||
CollisionShape2D firstOnRightBounds = firstOnRight.GetNode<CollisionShape2D>("Bounds");
|
||||
|
||||
_gridMarker = GetNode<GridMarker>("GridMarker");
|
||||
CollisionShape2D bounds = _playArea.GetNode<CollisionShape2D>("Bounds");
|
||||
int gridCellsX = (int)bounds.Shape.GetRect().Size.X / _cellSize, gridCellsY = (int)bounds.Shape.GetRect().Size.Y / _cellSize;
|
||||
for (int i = 0; i < gridCellsY; i++)
|
||||
CollisionShape2D playAreaBounds = _playArea.GetNode<CollisionShape2D>("Bounds");
|
||||
_sizeInPixels = new Vector2((int)playAreaBounds.Shape.GetRect().Size.X, (int)playAreaBounds.Shape.GetRect().Size.Y);
|
||||
_sizeInCells = new Vector2(_sizeInPixels.X / _cellSize, _sizeInPixels.Y / _cellSize);
|
||||
for (int i = 0; i < _sizeInCells.Y; i++)
|
||||
{
|
||||
_gridMarkers.Add([]);
|
||||
for (int j = 0; j < gridCellsX; j++)
|
||||
for (int j = 0; j < _sizeInCells.X; j++)
|
||||
{
|
||||
GridMarker newGridMarker = (GridMarker)_gridMarker.Duplicate();
|
||||
newGridMarker._address = new Vector2(j, i);
|
||||
newGridMarker.Position = new Vector2(_playArea.Position.X - bounds.Shape.GetRect().Size.X / 2 + (j+.5f)*_cellSize, _playArea.Position.Y - bounds.Shape.GetRect().Size.Y / 2 + (i+.5f)*_cellSize);
|
||||
newGridMarker.Modulate = new Color(((i+j)%2 == 0 ? "#ffffff" : "#000000")+(_showMarkers ? "ff" : "00"));
|
||||
newGridMarker.Position = new Vector2(_playArea.Position.X - playAreaBounds.Shape.GetRect().Size.X / 2 + (j+.5f)*_cellSize, _playArea.Position.Y - playAreaBounds.Shape.GetRect().Size.Y / 2 + (i+.5f)*_cellSize);
|
||||
_gridMarkers[i].Add(newGridMarker);
|
||||
AddChild(newGridMarker);
|
||||
}
|
||||
}
|
||||
|
||||
int leftEdgeSectionSizeY = (int)firstOnLeftBounds.Shape.GetRect().Size.Y, rightEdgeSectionSizeY = (int)firstOnRightBounds.Shape.GetRect().Size.Y;
|
||||
int leftEdgeSectionsCount = (int)_sizeInPixels.Y / leftEdgeSectionSizeY, rightEdgeSectionsCount = (int)_sizeInPixels.Y / rightEdgeSectionSizeY;
|
||||
|
||||
for (int i = 1; i < leftEdgeSectionsCount; i++)
|
||||
{
|
||||
StaticBody2D newLeftSection = (StaticBody2D)firstOnLeft.Duplicate();
|
||||
newLeftSection.Position += new Vector2(0, leftEdgeSectionSizeY * i);
|
||||
_leftEdges.Add(newLeftSection);
|
||||
AddChild(newLeftSection);
|
||||
}
|
||||
for (int i = 1; i < rightEdgeSectionsCount; i++)
|
||||
{
|
||||
StaticBody2D newRightSection = (StaticBody2D)firstOnRight.Duplicate();
|
||||
newRightSection.Position += new Vector2(0, rightEdgeSectionSizeY * i);
|
||||
_rightEdges.Add(newRightSection);
|
||||
AddChild(newRightSection);
|
||||
}
|
||||
|
||||
_gridMarker.QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user