Skip to content

Commit

Permalink
Merge pull request #56 from Michal78900/dev
Browse files Browse the repository at this point in the history
Dev to main
  • Loading branch information
Michal78900 authored Aug 5, 2022
2 parents 6d8e9bf + bb0db45 commit a149860
Show file tree
Hide file tree
Showing 204 changed files with 12,920 additions and 575 deletions.
10 changes: 10 additions & 0 deletions MapEditorReborn/API/Enums/BlockType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ public enum BlockType
/// Represents a sub-schematic.
/// </summary>
Schematic = 5,

/// <summary>
/// Represents a teleporter.
/// </summary>
Teleport = 6,

/// <summary>
/// Represents a locker.
/// </summary>
Locker = 7,
}
}
2 changes: 1 addition & 1 deletion MapEditorReborn/API/Features/MapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace MapEditorReborn.API.Features

using static API;

using Config = Config;
using Config = Configs.Config;

/// <summary>
/// A class which contains a few useful methods to interact with maps.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static int GetAttachmentsCode(string attachmentsString)

if (attachmentsString.Contains("+"))
{
string[] array = attachmentsString.Split(new char[] { '+' });
string[] array = attachmentsString.Split(new[] { '+' });

for (int j = 0; j < array.Length; j++)
{
Expand Down
9 changes: 3 additions & 6 deletions MapEditorReborn/API/Features/Objects/LightSourceObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ public LightSourceObject Init(LightSourceSerializable lightSourceSerializable, b
return this;
}

public LightSourceObject Init(SchematicBlockData block)
public override MapEditorObject Init(SchematicBlockData block)
{
IsSchematicBlock = true;
base.Init(block);

gameObject.name = block.Name;
gameObject.transform.localPosition = block.Position;

Base = new LightSourceSerializable(block.Properties["Color"].ToString(), float.Parse(block.Properties["Intensity"].ToString()), float.Parse(block.Properties["Range"].ToString()), bool.Parse(block.Properties["Shadows"].ToString()));
Base = new (block);

if (TryGetComponent(out LightSourceToy lightSourceToy))
Light = Light.Get(lightSourceToy);
Expand Down
91 changes: 68 additions & 23 deletions MapEditorReborn/API/Features/Objects/LockerObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,116 @@

namespace MapEditorReborn.API.Features.Objects
{
using System;
using System.Collections.Generic;
using System.Linq;
using Extensions;
using MapGeneration.Distributors;
using Mirror;
using Serializable;
using UnityEngine;

using static API;
using Random = UnityEngine.Random;

public class LockerObject : MapEditorObject
{
private void Awake()
{
StructurePositionSync = GetComponent<StructurePositionSync>();
}

public LockerObject Init(LockerSerializable lockerSerializable, bool first = false)
{
Base = lockerSerializable;

if (TryGetComponent(out Locker locker))
{
Locker = locker;
Locker.Loot = System.Array.Empty<LockerLoot>();
Locker.Loot = Array.Empty<LockerLoot>();
Base.LockerType = Locker.GetLockerType();
}

if (first)
Base.KeycardPermissions = Locker.Chambers[0].RequiredPermissions;

HandleItems();
NetworkServer.Spawn(gameObject);

return this;
}

public override MapEditorObject Init(SchematicBlockData block)
{
base.Init(block);

Base = new LockerSerializable(block);

if (TryGetComponent(out Locker locker))
{
Locker = locker;
Locker.Loot = Array.Empty<LockerLoot>();
}

HandleItems();

return this;
}

private void HandleItems()
{
foreach (LockerChamber lockerChamber in Locker.Chambers)
lockerChamber.RequiredPermissions = Base.KeycardPermissions;

Dictionary<int, List<LockerItemSerializable>> chambersCopy = null;
if (Base.ShuffleChambers)
{
chambersCopy = new(Base.Chambers.Count);
List<List<LockerItemSerializable>> chambersRandomValues = Base.Chambers.Values.OrderBy(x => Random.value).ToList();
for (int i = 0; i < Base.Chambers.Count; i++)
{
chambersCopy.Add(i, chambersRandomValues[i]);
}
}

for (int i = 0; i < Locker.Chambers.Length; i++)
{
if (i >= Base.Chambers.Count)
if (i == Base.Chambers.Count)
break;

LockerChamberSerializable choosedLoot = Choose(Base.Chambers[i]);
Locker.Chambers[i].SpawnItem(choosedLoot.Item, choosedLoot.Count);
}
LockerItemSerializable chosenLoot = Choose(Base.ShuffleChambers ? chambersCopy[i] : Base.Chambers[i]);
if (chosenLoot == null)
continue;

NetworkServer.Spawn(gameObject);
Locker.Chambers[i].SpawnItem(chosenLoot.Item, chosenLoot.Count);
}

return this;
Locker.NetworkOpenedChambers = Base.OpenedChambers;
_usedChambers = new(Locker.Chambers.Length);
}

public LockerSerializable Base;

public Locker Locker { get; private set; }

public StructurePositionSync StructurePositionSync { get; private set; }

/// <inheritdoc cref="MapEditorObject.UpdateObject()"/>
public override void UpdateObject()
{
MapEditorObject newLocker = ObjectSpawner.SpawnPropertyObject(Position, this);
SpawnedObjects[SpawnedObjects.IndexOf(this)] = newLocker;

if (prevOwner != null)
Events.Handlers.Internal.ToolGunHandler.SelectObject(prevOwner, newLocker);

Destroy(gameObject);
StructurePositionSync.Network_position = Position;
StructurePositionSync.Network_rotationY = (sbyte)Mathf.RoundToInt(transform.eulerAngles.y / 5.625f);
base.UpdateObject();
}

private static LockerChamberSerializable Choose(List<LockerChamberSerializable> chambers)
internal HashSet<LockerChamber> _usedChambers;

private static LockerItemSerializable Choose(List<LockerItemSerializable> chambers)
{
if (chambers == null || chambers.Count == 0)
return null;

float total = 0;

foreach (LockerChamberSerializable elem in chambers)
foreach (LockerItemSerializable elem in chambers)
{
total += elem.Chance;
}
Expand All @@ -82,13 +129,11 @@ private static LockerChamberSerializable Choose(List<LockerChamberSerializable>
{
return chambers[i];
}
else
{
randomPoint -= chambers[i].Chance;
}

randomPoint -= chambers[i].Chance;
}

return chambers[chambers.Count - 1];
}
}
}
}
22 changes: 20 additions & 2 deletions MapEditorReborn/API/Features/Objects/MapEditorObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace MapEditorReborn.API.Features.Objects
using Exiled.API.Enums;
using Exiled.API.Features;
using Mirror;
using Serializable;
using UnityEngine;

/// <summary>
Expand All @@ -38,6 +39,23 @@ public virtual void UpdateObject()
NetworkServer.Spawn(gameObject);
}

public virtual MapEditorObject Init(SchematicBlockData block)
{
IsSchematicBlock = true;

GameObject gO = gameObject;
gO.name = block.Name;
gO.transform.localPosition = block.Position;

if (IsRotatable)
gO.transform.localEulerAngles = block.Rotation;

if (IsScalable)
gO.transform.localScale = block.Scale;

return this;
}

/// <summary>
/// Gets the attached <see cref="IndicatorObject"/> of the object.
/// </summary>
Expand Down Expand Up @@ -193,7 +211,7 @@ public Room FindRoom()
public static Color GetColorFromString(string colorText)
{
Color color = new(-1f, -1f, -1f);
string[] charTab = colorText.Split(new char[] { ':' });
string[] charTab = colorText.Split(new[] { ':' });

if (charTab.Length >= 4)
{
Expand Down Expand Up @@ -230,4 +248,4 @@ public static Color GetColorFromString(string colorText)

private RoomType _forcedRoom = RoomType.Unknown;
}
}
}
32 changes: 6 additions & 26 deletions MapEditorReborn/API/Features/Objects/PrimitiveObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace MapEditorReborn.API.Features.Objects
using System.Collections.Generic;
using AdminToys;
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.API.Features.Toys;
using Features.Serializable;
using MEC;
Expand Down Expand Up @@ -53,20 +54,12 @@ public PrimitiveObject Init(PrimitiveSerializable primitiveSerializable)
return this;
}

public PrimitiveObject Init(SchematicBlockData block)
public override MapEditorObject Init(SchematicBlockData block)
{
IsSchematicBlock = true;
base.Init(block);

gameObject.name = block.Name;
gameObject.transform.localPosition = block.Position;
gameObject.transform.localEulerAngles = block.Rotation;
gameObject.transform.localScale = block.Scale;

Base = new PrimitiveSerializable(
(PrimitiveType)Enum.Parse(typeof(PrimitiveType), block.Properties["PrimitiveType"].ToString()),
block.Properties["Color"].ToString());

_primitiveObjectToy.NetworkMovementSmoothing = 60;
Base = new (block);
Primitive.MovementSmoothing = 60;

UpdateObject();

Expand Down Expand Up @@ -110,26 +103,13 @@ public override void UpdateObject()
_primitiveObjectToy.NetworkPrimitiveType = Base.PrimitiveType;
_primitiveObjectToy.NetworkMaterialColor = GetColorFromString(Base.Color);

if (!IsSchematicBlock && _prevScale != transform.localScale)
if (!IsSchematicBlock || _prevScale != transform.localScale)
{
_prevScale = transform.localScale;
base.UpdateObject();
}
}

private IEnumerator<float> Decay()
{
yield return Timing.WaitForSeconds(1f);

while (Primitive.Color.a > 0)
{
Primitive.Color = new Color(Primitive.Color.r, Primitive.Color.g, Primitive.Color.b, Primitive.Color.a - 0.05f);
yield return Timing.WaitForSeconds(0.1f);
}

Destroy();
}

private Vector3 _prevScale;
}
}
Loading

0 comments on commit a149860

Please sign in to comment.