Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Überarbeitung von Entities, Komponenten und Extensions #245

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions OctoAwesome/OctoAwesome.Basics/ComplexPlanet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public class ComplexPlanet : Planet
/// <param name="id">ID des Planeten</param>
/// <param name="universe">ID des Universums</param>
/// <param name="size">Größe des Planeten in Zweierpotenzen Chunks</param>
/// <param name="generator">Instanz des Map-Generators</param>
/// <param name="seed">Seed des Zufallsgenerators</param>
public ComplexPlanet(int id, Guid universe, Index3 size, IMapGenerator generator, int seed)
: base(id, universe, size, seed)
public ComplexPlanet(int id, Guid universe, Index3 size, int seed) : base(id, universe, size, seed)
{
Initalize();
BiomeGenerator = new SurfaceBiomeGenerator(this, 40);
ClimateMap = new Climate.ComplexClimateMap(this);
}

public ComplexPlanet() : base()
Expand All @@ -36,11 +35,6 @@ public ComplexPlanet() : base()
public override void Deserialize(Stream stream)
{
base.Deserialize(stream);
Initalize();
}

private void Initalize()
{
Copy link
Member

@XYZLassi XYZLassi Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warum Initalize entfernen ???

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hast recht der muss wieder rein!

BiomeGenerator = new SurfaceBiomeGenerator(this, 40);
ClimateMap = new Climate.ComplexClimateMap(this);
}
Expand Down
2 changes: 1 addition & 1 deletion OctoAwesome/OctoAwesome.Basics/ComplexPlanetGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ComplexPlanetGenerator : IMapGenerator
public IPlanet GeneratePlanet(Guid universe, int id, int seed)
{
Index3 size = new Index3(12, 12, 3);
ComplexPlanet planet = new ComplexPlanet(id, universe, size, this, seed)
ComplexPlanet planet = new ComplexPlanet(id, universe, size, seed)
{
Generator = this
};
Expand Down
124 changes: 124 additions & 0 deletions OctoAwesome/OctoAwesome.Basics/Controls/InventoryControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using engenious;
using engenious.Graphics;
using MonoGameUi;
using OctoAwesome.Basics.EntityComponents;
using OctoAwesome.Entities;
using System;
using System.Collections.Generic;

namespace OctoAwesome.Basics.Controls
{
internal sealed class InventoryControl : Panel
{
private const int COLUMNS = 8;

/// <summary>
/// Gibt den aktuell selektierten Slot an.
/// </summary>
public InventorySlot HoveredSlot { get; private set; }

private InventoryComponent inventory;
private Label volumeLabel;
private Label massLabel;
private Label nameLabel;

public InventoryControl(BaseScreenComponent manager, IUserInterfaceExtensionManager interfacemanager,
InventoryComponent inventory) : base(manager)
{
this.inventory = inventory;

Texture2D panelBackground = interfacemanager.LoadTextures(manager.GetType(), "panel");
Margin = new Border(0, 0, 0, 0);
HorizontalAlignment = HorizontalAlignment.Stretch;
VerticalAlignment = VerticalAlignment.Stretch;

Grid parentgrid = new Grid(manager);
parentgrid.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Parts, Width = 1, });
parentgrid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Parts, Height = 1, });
parentgrid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Fixed, Height = 100, });
Controls.Add(parentgrid);

Panel panel = new Panel(manager)
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Background = NineTileBrush.FromSingleTexture(panelBackground, 30, 30)
};
parentgrid.AddControl(panel, 0, 0);

ScrollContainer scroll = new ScrollContainer(manager)
{
Margin = Border.All(0, 5, 5, 5),
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
};
panel.Controls.Add(scroll);

StackPanel infoPanel = new StackPanel(manager)
{
Margin = new Border(0, 10, 0, 0),
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Background = NineTileBrush.FromSingleTexture(panelBackground, 30, 30),
};

nameLabel = new Label(manager);
infoPanel.Controls.Add(nameLabel);
massLabel = new Label(manager);
infoPanel.Controls.Add(massLabel);
volumeLabel = new Label(manager);
infoPanel.Controls.Add(volumeLabel);
parentgrid.AddControl(infoPanel, 0, 1);

Grid grid = new Grid(manager)
{
HorizontalAlignment = HorizontalAlignment.Stretch,
};
for (int i = 0; i < COLUMNS; i++)
grid.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Parts, Width = 1 });

int rows = (int) System.Math.Ceiling((float) inventory.Inventory.Count / COLUMNS);
for (int i = 0; i < rows; i++)
grid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Fixed, Height = 50 });

int column = 0;
int row = 0;
foreach (var item in inventory.Inventory)
{
Texture2D texture = interfacemanager.LoadTextures(item.Definition.GetType(), item.Definition.Icon);

var image = new Image(manager) { Texture = texture, Width = 42, Height = 42, VerticalAlignment = VerticalAlignment.Center };
image.MouseEnter += (s, e) => { HoveredSlot = item; };
image.MouseLeave += (s, e) => { HoveredSlot = null; };
image.StartDrag += (e) =>
{
e.Handled = true;
e.Icon = texture;
e.Content = item;
e.Sender = image;
};
var label = new Label(manager) { Text = item.Amount.ToString(), HorizontalAlignment = HorizontalAlignment.Right, VerticalTextAlignment = VerticalAlignment.Bottom, Background = new BorderBrush(Color.White) };
grid.AddControl(image, column, row);
grid.AddControl(label, column, row);

column++;
if (column >= COLUMNS)
{
row++;
column = 0;
}
}
scroll.Content = grid;
}
protected override void OnUpdate(GameTime gameTime)
{
nameLabel.Text = HoveredSlot?.Definition.Name ?? "";
massLabel.Text = volumeLabel.Text = HoveredSlot?.Amount.ToString() ?? "";
}
public override string ToString()
{
// TODO: resx -> Languages.OctoBasics.Inventory or what ever...
return "Inventar";
}
}
}
183 changes: 183 additions & 0 deletions OctoAwesome/OctoAwesome.Basics/Controls/ToolbarControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using MonoGameUi;
using System.Collections.Generic;
using engenious;
using engenious.Graphics;
using OctoAwesome.Entities;
using System;
using OctoAwesome.Basics.EntityComponents;

namespace OctoAwesome.Basics.Controls
{
internal class ToolbarControl : Panel
{
private Button[] buttons;

private Image[] images;

private Brush buttonBackgroud;

private Brush activeBackground;

private ToolBarComponent toolbar;

private Label activeToolLabel;

private Dictionary<string, Texture2D> toolTextures;

public ToolbarControl(BaseScreenComponent screenManager, IUserInterfaceExtensionManager manager, ToolBarComponent bar) :
base(screenManager)
{
HorizontalAlignment = HorizontalAlignment.Stretch;
VerticalAlignment = VerticalAlignment.Bottom;
Height = 100;
toolbar = bar;
buttonBackgroud = new BorderBrush(Color.Black);
activeBackground = new BorderBrush(Color.Red);
buttons = new Button[toolbar.Tools.Length];
images = new Image[toolbar.Tools.Length];

toolTextures = new Dictionary<string, Texture2D>();

foreach (var item in bar.Service.DefinitionManager.GetDefinitions())
{
Texture2D texture = manager.LoadTextures(item.GetType(), item.Icon);
toolTextures.Add(item.GetType().FullName, texture);
}

Grid grid = new Grid(screenManager)
{
Margin = new Border(0, 0, 0, 0),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom
};
Controls.Add(grid);

grid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Auto, Height = 1 });
grid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Fixed, Height = 50 });

for (int i = 0; i < toolbar.Tools.Length; i++)
{
grid.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Fixed, Width = 50 });
}

activeToolLabel = new Label(screenManager);
activeToolLabel.VerticalAlignment = VerticalAlignment.Top;
activeToolLabel.HorizontalAlignment = HorizontalAlignment.Center;
activeToolLabel.Background = new BorderBrush(Color.Black * 0.3f);
activeToolLabel.TextColor = Color.White;
grid.AddControl(activeToolLabel, 0, 0, 10);

for (int i = 0; i < toolbar.Tools.Length; i++)
{
buttons[i] = new Button(screenManager)
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Background = buttonBackgroud,
HoveredBackground = null,
PressedBackground = null,
};
buttons[i].Content = images[i] = new Image(screenManager)
{
Width = 42,
Height = 42,
};
grid.AddControl(buttons[i], i, 1);
}
}
protected override void OnUpdate(GameTime gameTime)
{
if (!Visible || !Enabled)
return;
if(!(toolbar.Entity is IControllable con) || con.Controller == null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laut unseren Codingrichtlinen sind out Variablen nur innerhalb des if's scopes zu verwenden

Copy link
Author

@HierGibtEsDrachen HierGibtEsDrachen Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Das ist im selben Scope. Da C# bei If und || operatoren abbricht bzw. nicht mehr weiter prüft sobald die Bedingung erfühlt ist.
Zweitens das ist kein out... das ist immer noch ein is. Selbst wenn C#7 das aufgemotzt hat.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ja ok das ist kein out habe ich übersehen. Aber nein das ist nicht der selbe Scope ^^ du returnst direkt unter dem if und verwendest somit das con außerhalb des if's s.h. zeile 101 oder 122. Das ist etwas was auch ich mir abgewöhnen muss wenn ich in octo programmiere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok OK

return;
#region Toolbar Update

if (toolbar.Tools != null && toolbar.Tools.Length > 0)
{
if (toolbar.ActiveTool == null) toolbar.ActiveTool = toolbar.Tools[0];
for (int i = 0; i < Math.Min(toolbar.Tools.Length, con.Controller.SlotInput.Length); i++)
{
if (con.Controller.SlotInput[i]) toolbar.ActiveTool = toolbar.Tools[i];
con.Controller.SlotInput[i] = false;
}
}

//Index des aktiven Werkzeugs ermitteln
int activeTool = -1;
List<int> toolIndices = new List<int>();
for (int i = 0; i < toolbar.Tools.Length; i++)
{

if (toolbar.ActiveTool != null && toolbar.ActiveTool.Amount <= 0)
toolbar.RemoveSlot(toolbar.ActiveTool);

if (toolbar.Tools[i] != null)
toolIndices.Add(i);

if (toolbar.Tools[i] == toolbar.ActiveTool)
activeTool = toolIndices.Count - 1;
}

if (con.Controller.SlotLeftInput)
{
if (activeTool > -1)
activeTool--;
else if (toolIndices.Count > 0)
activeTool = toolIndices[toolIndices.Count - 1];
}
con.Controller.SlotLeftInput = false;

if (con.Controller.SlotRightInput)
{
if (activeTool > -1)
activeTool++;
else if (toolIndices.Count > 0)
activeTool = toolIndices[0];
}
con.Controller.SlotRightInput = false;

if (activeTool > -1)
{
activeTool = (activeTool + toolIndices.Count) % toolIndices.Count;
toolbar.ActiveTool = toolbar.Tools[toolIndices[activeTool]];
}
#endregion

// Aktualisierung des aktiven Buttons
for (int i = 0; i < toolbar.Tools.Length; i++)
{
if (toolbar.Tools != null &&
toolbar.Tools.Length > i &&
toolbar.Tools[i] != null &&
toolbar.Tools[i].Definition != null)
{
images[i].Texture = toolTextures[toolbar.Tools[i].Definition.GetType().FullName];
if (toolbar.ActiveTool == toolbar.Tools[i])
buttons[i].Background = activeBackground;
else
buttons[i].Background = buttonBackgroud;
}
else
{
images[i].Texture = null;
buttons[i].Background = buttonBackgroud;
}
}

//Aktualisierung des ActiveTool Labels
activeToolLabel.Text = toolbar.ActiveTool != null ?
string.Format("{0} ({1})", toolbar.ActiveTool.Definition.Name, toolbar.ActiveTool.Amount) :
string.Empty;

activeToolLabel.Visible = !(activeToolLabel.Text == string.Empty);

base.OnUpdate(gameTime);
}
public override string ToString()
{
// TODO: resx -> Languages.OctoBasics.Tool or what ever...
return "Werkzeuge";
}
}
}
Loading