Skip to content

Commit

Permalink
Enable item actions for multiplayer, fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejNepozitek committed Aug 12, 2017
1 parent 31f4672 commit 5bcfa45
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 44 deletions.
12 changes: 6 additions & 6 deletions Assets/Scripts/FPS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public class FPS : MonoBehaviour

void Start()
{
/*myText = GetComponent<Text>();
myText = GetComponent<Text>();
if (!myText)
{
Debug.Log("UtilityFramesPerSecond needs a GUIText component!");
enabled = false;
return;
}
timeleft = updateInterval;*/
timeleft = updateInterval;
}

void Update()
{
/*timeleft -= Time.deltaTime;
timeleft -= Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;

Expand All @@ -49,17 +49,17 @@ void Update()
string format = System.String.Format("{0:F2} FPS", fps);
myText.text = format;

if (fps < 30)
/*if (fps < 30)
myText.material.color = Color.yellow;
else
if (fps < 10)
myText.material.color = Color.red;
else
myText.material.color = Color.green;
myText.material.color = Color.green;*/
// DebugConsole.Log(format,level);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}*/
}
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void StartGame()
systems
.Add(new ProcessBasicMoveSystem(contexts))
.Add(new SpawnItemSystem(contexts))
// .Add(new EquipItemSystem(contexts)) TODO: enable
.Add(new EquipItemSystem(contexts))

// React to components changes
.Add(new ViewSystems(contexts)) // May need to be revised
Expand Down
17 changes: 8 additions & 9 deletions Assets/Sources/Extensions/ActionsContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,23 @@ public static ActionsEntity BasicMove(this ActionsContext context, GameEntity ta

public static ActionsEntity SpawnItem(this ActionsContext context, IItem item, IntVector2 position)
{
var entity = context.CreateEntity();

entity.AddAction(new SpawnItemAction() { Item = item, Position = position });

return entity;
return context.SpawnItem(item.Name, position);
}

public static ActionsEntity SpawnItem(this ActionsContext context, ItemName name, IntVector2 position)
{
var item = ItemDatabase.Instance.GetItem(name);
return context.SpawnItem(item, position);
var entity = context.CreateEntity();

entity.AddAction(new SpawnItemAction() { Item = name, Position = position });

return entity;
}

public static ActionsEntity Equip(this ActionsContext context, IItem item, GameEntity target)
{
var entity = context.CreateEntity();

entity.AddAction(new EquipAction() { Item = item, Target = target });
entity.AddAction(new EquipAction() { Item = item.Name, Entity = target.GetReference() });

return entity;
}
Expand All @@ -50,7 +49,7 @@ public static ActionsEntity PickAndEquip(this ActionsContext context, IntVector2
{
var entity = context.CreateEntity();

entity.AddAction(new PickAndEquipAction() { Position = position, Target = target });
entity.AddAction(new PickAndEquipAction() { Position = position, Entity = target.GetReference() });

return entity;
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/Sources/Features/Actions/IAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

[ProtoContract]
[ProtoInclude(1, typeof(BasicMoveAction))]
[ProtoInclude(2, typeof(SpawnItemAction))]
[ProtoInclude(3, typeof(EquipAction))]
[ProtoInclude(4, typeof(PickAndEquipAction))]
public interface IAction
{
bool Validate(GameContext context);
Expand Down
14 changes: 10 additions & 4 deletions Assets/Sources/Features/Items/EquipAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
public class EquipAction : IAction
using ProtoBuf;

[ProtoContract]
public class EquipAction : IAction
{
public IItem Item;
public GameEntity Target;
[ProtoMember(1)]
public ItemName Item;

[ProtoMember(2)]
public EntityReference Entity;

public bool Validate(GameContext context)
{
throw new System.NotImplementedException();
return true;
}
}
11 changes: 3 additions & 8 deletions Assets/Sources/Features/Items/EquipItemSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,17 @@ protected override void Execute(List<ActionsEntity> entities)
if (entity.action.Action is EquipAction)
{
var action = (EquipAction)entity.action.Action;
item = action.Item;
target = action.Target;
item = ItemDatabase.Instance.GetItem(action.Item);
target = action.Entity.GetEntity();
}
else
{
var action = (PickAndEquipAction)entity.action.Action;
var itemEntity = Map.Instance.GetItem(action.Position);

if (itemEntity == null)
{
continue; // This should be somewhere else
}

itemEntity.isDestroyed = true;
item = itemEntity.item.Item;
target = action.Target;
target = action.Entity.GetEntity();
}

// TODO: should old game object be destroyed while replacing?
Expand Down
20 changes: 17 additions & 3 deletions Assets/Sources/Features/Items/PickAndEquipAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
public class PickAndEquipAction : IAction
using ProtoBuf;

[ProtoContract]
public class PickAndEquipAction : IAction
{
[ProtoMember(1)]
public IntVector2 Position;
public GameEntity Target;

[ProtoMember(2)]
public EntityReference Entity;

/// <summary>
/// Entity's position should be the same a the item's position
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public bool Validate(GameContext context)
{
throw new System.NotImplementedException();
if (Entity.GetEntity().position.value != Position) return false;
if (Map.Instance.GetItem(Position) == null) return false;

return true;
}
}
2 changes: 1 addition & 1 deletion Assets/Sources/Features/Items/RegisterItemsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public void Initialize()
var items = ItemDatabase.Instance;
items.Reset();

items.RegisterItem(ItemName.IronAxe, new Weapon(Prefabs.IronAxe, 10));
items.RegisterItem(new Weapon(ItemName.IronAxe, Prefabs.IronAxe, 10));
}
}
12 changes: 9 additions & 3 deletions Assets/Sources/Features/Items/SpawnItemAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
public class SpawnItemAction : IAction
using ProtoBuf;

[ProtoContract]
public class SpawnItemAction : IAction
{
public IItem Item;
[ProtoMember(1)]
public ItemName Item;

[ProtoMember(2)]
public IntVector2 Position;

public bool Validate(GameContext context)
{
throw new System.NotImplementedException();
return true;
}
}
2 changes: 1 addition & 1 deletion Assets/Sources/Features/ProcGen/RectangularMapSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void Initialize()
}

//context.CreateItem(ItemName.IronAxe, new IntVector2(10, 10));
//actionsContext.SpawnItem(ItemName.IronAxe, new IntVector2(10, 10)); TODO: uncomment
actionsContext.SpawnItem(ItemName.IronAxe, new IntVector2(10, 10));
{
var networkEntity = NetworkController.Instance.NetworkEntity;
if (networkEntity != null) // TODO: ugly
Expand Down
5 changes: 4 additions & 1 deletion Assets/Sources/Helpers/Items/IArmor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
public interface IArmor : IItem
public abstract class Equipment : IItem
{
public ItemName Name { get; protected set; }
public string Prefab { get; protected set; }
public abstract InventorySlot Slot { get; }
}
1 change: 1 addition & 0 deletions Assets/Sources/Helpers/Items/IItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public interface IItem
{
ItemName Name { get; }
string Prefab { get; }
InventorySlot Slot { get; }
}
4 changes: 2 additions & 2 deletions Assets/Sources/Helpers/Items/ItemDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public IItem GetItem(ItemName name)
return items[name];
}

public void RegisterItem(ItemName name, IItem item)
public void RegisterItem(IItem item)
{
items.Add(name, item);
items.Add(item.Name, item);
}
}
1 change: 1 addition & 0 deletions Assets/Sources/Helpers/Items/ItemName.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public enum ItemName
{
Unknown = 0, // For protobuf
IronAxe
}
9 changes: 4 additions & 5 deletions Assets/Sources/Helpers/Items/Weapon.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
public class Weapon : IArmor
public class Weapon : Equipment
{
public InventorySlot Slot
public override InventorySlot Slot
{
get
{
return InventorySlot.Weapon;
}
}

public string Prefab { get; private set; }

public float Attack { get; private set; }

public Weapon(string prefab, float attack)
public Weapon(ItemName name, string prefab, float attack)
{
Name = name;
Prefab = prefab;
Attack = attack;
}
Expand Down

0 comments on commit 5bcfa45

Please sign in to comment.