added rocket

This commit is contained in:
dddushesss 2022-02-07 13:16:46 +03:00
parent 53ffc3a8f6
commit e574b0294a
14 changed files with 43035 additions and 66 deletions

View File

@ -26,7 +26,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5503245665281670417} m_GameObject: {fileID: 5503245665281670417}
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068} m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 1, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.11548259, y: 0.11548259, z: 0.11548259} m_LocalScale: {x: 0.11548259, y: 0.11548259, z: 0.11548259}
m_Children: m_Children:
- {fileID: 1687892424394123116} - {fileID: 1687892424394123116}
@ -40,7 +40,7 @@ MeshFilter:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5503245665281670417} m_GameObject: {fileID: 5503245665281670417}
m_Mesh: {fileID: 4300000, guid: e161d2913bca37a44a645d97c1e0c715, type: 3} m_Mesh: {fileID: 10208, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &5478883122182798693 --- !u!23 &5478883122182798693
MeshRenderer: MeshRenderer:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -115,7 +115,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 460430, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, type: 3} - target: {fileID: 460430, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, type: 3}
propertyPath: m_LocalPosition.y propertyPath: m_LocalPosition.y
value: -6.920007 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 460430, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, type: 3} - target: {fileID: 460430, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, type: 3}
propertyPath: m_LocalPosition.z propertyPath: m_LocalPosition.z
@ -169,7 +169,6 @@ PrefabInstance:
m_SourcePrefab: {fileID: 100100000, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, type: 3}
--- !u!4 &1687892424394123116 stripped --- !u!4 &1687892424394123116 stripped
Transform: Transform:
m_CorrespondingSourceObject: {fileID: 460430, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, m_CorrespondingSourceObject: {fileID: 460430, guid: 3b671081e44be1c4aa4355e8ba6e8a5e, type: 3}
type: 3}
m_PrefabInstance: {fileID: 1687892424393925090} m_PrefabInstance: {fileID: 1687892424393925090}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}

View File

@ -23,6 +23,8 @@ MonoBehaviour:
_spawnChance: 0.705 _spawnChance: 0.705
- item: {fileID: 11400000, guid: 98f59e15ea7ad2d47b2e3ffd67e2a650, type: 2} - item: {fileID: 11400000, guid: 98f59e15ea7ad2d47b2e3ffd67e2a650, type: 2}
_spawnChance: 0.64 _spawnChance: 0.64
- item: {fileID: 11400000, guid: 7605998bd1a481543a96857fa015169b, type: 2}
_spawnChance: 0.848
- item: {fileID: 11400000, guid: 133e523fdd159754e8bf8927faec5b0f, type: 2} - item: {fileID: 11400000, guid: 133e523fdd159754e8bf8927faec5b0f, type: 2}
_spawnChance: 0.833 _spawnChance: 0.833
- item: {fileID: 11400000, guid: 133e523fdd159754e8bf8927faec5b0f, type: 2} - item: {fileID: 11400000, guid: 133e523fdd159754e8bf8927faec5b0f, type: 2}

View File

@ -0,0 +1,30 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 30d4ee04b4d04d47ad8f932a004c1728, type: 3}
m_Name: Rocket
m_EditorClassIdentifier:
icon: {fileID: 21300000, guid: f591ebc6dc1028948aa740d86f44adb9, type: 3}
type: 0
_weapon:
name: Rocket
icon: {fileID: 0}
objectToThrow: {fileID: 5503245665281670417, guid: 857de15730a382b48a9d497d078336cd, type: 3}
VFXGameObject: {fileID: 8032726150168125150, guid: 65c8920b6d1553a4d885992e8f7dff6a, type: 3}
modifiedDamage: 0
damage: 100
speed: 1
disnatce: 10
reloadTime: 1
shots: 1
shotSound: {fileID: 0}
hitSound: {fileID: 0}
_aimGameObject: {fileID: 2273039178377770117, guid: 09a0317cbdff9fa479a18c9e20743a8e, type: 3}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7605998bd1a481543a96857fa015169b
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -87,6 +87,10 @@ namespace GameUI
ability.Invoke(() => SwitchButton(button)); ability.Invoke(() => SwitchButton(button));
OnBuildingInvoked?.Invoke(ability); OnBuildingInvoked?.Invoke(ability);
break; break;
case SpecialWeapon specialWeapon:
specialWeapon.Invoke(() => SwitchButton(button));
OnBuildingInvoked?.Invoke(specialWeapon);
break;
} }
}); });
} }

View File

@ -10,6 +10,7 @@ namespace HexFiled
public class HexCell : MonoBehaviour public class HexCell : MonoBehaviour
{ {
public HexCoordinates coordinates; public HexCoordinates coordinates;
public int index;
public event Action<HexCell> OnHexPainted; public event Action<HexCell> OnHexPainted;
@ -40,11 +41,19 @@ namespace HexFiled
} }
} }
public SerializibleHexCell ToSerializibleHexCell()
{
SerializibleHexCell cell = new SerializibleHexCell();
cell.HexCoordinates = coordinates;
cell.index = index;
return cell;
}
private void Awake() private void Awake()
{ {
_renderer = GetComponent<MeshRenderer>(); _renderer = GetComponent<MeshRenderer>();
_color = UnitColor.Grey; _color = UnitColor.Grey;
if (HexManager.CellByColor == null) return;
if (!HexManager.CellByColor.ContainsKey(_color)) if (!HexManager.CellByColor.ContainsKey(_color))
{ {
HexManager.CellByColor.Add(_color, new List<HexCell>() { this }); HexManager.CellByColor.Add(_color, new List<HexCell>() { this });
@ -69,8 +78,14 @@ namespace HexFiled
public void SetNeighbor(HexDirection direction, HexCell cell) public void SetNeighbor(HexDirection direction, HexCell cell)
{ {
neighbors ??= new HexCell[6];
neighbors[(int)direction] = cell; neighbors[(int)direction] = cell;
if (cell == null) return;
cell.neighbors ??= new HexCell[6];
cell.neighbors[(int)direction.Back()] = this; cell.neighbors[(int)direction.Back()] = this;
cell.neighbors ??= new HexCell[6];
} }
public void PaintHex(UnitColor color, bool isSetting = false) public void PaintHex(UnitColor color, bool isSetting = false)
@ -83,9 +98,9 @@ namespace HexFiled
} }
_renderer.material.mainTexture = HexGrid.Colors[color].Texture; _renderer.material.mainTexture = HexGrid.Colors[color].Texture;
HexManager.CellByColor[_color].Remove(this); HexManager.CellByColor[_color].Remove(this);
_color = color; _color = color;
HexManager.CellByColor[_color].Add(this); HexManager.CellByColor[_color].Add(this);
if (!isSetting) if (!isSetting)

View File

@ -1,9 +1,10 @@
using UnityEngine; using System;
using UnityEngine;
namespace HexFiled namespace HexFiled
{ {
[System.Serializable] [System.Serializable]
public struct HexCoordinates { public struct HexCoordinates : IComparable<HexCoordinates> {
[SerializeField] [SerializeField]
private int x, z; private int x, z;
@ -44,6 +45,10 @@ namespace HexFiled
return new HexCoordinates(x - z / 2, z); return new HexCoordinates(x - z / 2, z);
} }
public static (int x, int z) ToOffsetCoordinates(HexCoordinates coordinates)
{
return (coordinates.X + coordinates.Z / 2, coordinates.Z);
}
public static HexCoordinates FromPosition (Vector3 position) { public static HexCoordinates FromPosition (Vector3 position) {
float x = position.x / (HexMetrics.innerRadius * 2f); float x = position.x / (HexMetrics.innerRadius * 2f);
float y = -x; float y = -x;
@ -72,12 +77,18 @@ namespace HexFiled
return new HexCoordinates(iX, iZ); return new HexCoordinates(iX, iZ);
} }
public static Vector3 ToPosition (HexCoordinates position) { public static Vector3 ToPosition(HexCoordinates position) {
/*
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
*/
var hexPos = ToOffsetCoordinates(position);
Vector3 pos; Vector3 pos;
pos.x = (position.x + position.z * 0.5f - position.z / 2) * (HexMetrics.innerRadius * 2f); pos.x = (hexPos.x + hexPos.z * 0.5f - hexPos.z / 2) * (HexMetrics.innerRadius * 2f);
pos.y = 0f; pos.y = 0f;
pos.z = position.z * (HexMetrics.outerRadius * 1.5f); pos.z = hexPos.z * (HexMetrics.outerRadius * 1.5f);
return pos; return pos;
} }
@ -86,8 +97,17 @@ namespace HexFiled
X.ToString() + ", " + Y.ToString() + ", " + Z.ToString() + ")"; X.ToString() + ", " + Y.ToString() + ", " + Z.ToString() + ")";
} }
public string ToStringOnSeparateLines () { public string ToStringOnSeparateLines () {
return X.ToString() + "\n" + Y.ToString() + "\n" + Z.ToString(); return X.ToString() + "\n" + Y.ToString() + "\n" + Z.ToString();
} }
public int CompareTo(HexCoordinates other)
{
var xComparison = x.CompareTo(other.X);
if (xComparison != 0) return xComparison;
return z.CompareTo(other.Z);
}
} }
} }

View File

@ -1,61 +1,143 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using HexFiled; using HexFiled;
using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.Purchasing.MiniJSON; using UnityEngine.Purchasing.MiniJSON;
using Object = UnityEngine.Object;
namespace DefaultNamespace namespace DefaultNamespace
{ {
[Serializable]
public class GridToSave
{
public SerializibleHexCell[] cells;
public int height;
public int width;
}
[Serializable]
public class SerializibleHexCell
{
public HexCoordinates HexCoordinates;
public int index;
}
public class HexMapEditor : MonoBehaviour public class HexMapEditor : MonoBehaviour
{ {
[SerializeField] private GameObject hexPrefab; [SerializeField] private GameObject hexPrefab;
[SerializeField] private GameObject labelPrefab; [SerializeField] private TMP_Text labelPrefab;
[SerializeField] private GameObject gridCanvas;
private GameObject gridCanvasInstance;
private HexCell[] _cells;
private HexGrid hexGrid; private HexGrid hexGrid;
private int _width;
private int _height;
private Color activeColor; private Color activeColor;
private GameObject fieldBaseGameObject; private GameObject fieldBaseGameObject;
private GameObject labelCanvas;
[EditorButton] [EditorButton]
private void DrawMap(int x, int y) private void DrawMap(int x, int y)
{ {
if (hexGrid != null) if (fieldBaseGameObject != null)
{ {
Destroy(fieldBaseGameObject); DestroyImmediate(fieldBaseGameObject);
} }
if (gridCanvasInstance == null)
WriteToBinaryFile("HexField", hexGrid); {
fieldBaseGameObject = hexGrid.SpawnField(); DestroyImmediate(gridCanvasInstance);
}
gridCanvasInstance = Instantiate(gridCanvas);
fieldBaseGameObject = new GameObject("HexField");
_cells = new HexCell[x * y];
_width = x;
_height = y;
SpawnField();
} }
[EditorButton] [EditorButton]
private void LoadMap(string name) private void LoadMap(string fileName)
{ {
if (File.Exists(name)) if (File.Exists(Application.persistentDataPath
+ $"/{fileName}.dat"))
{ {
hexGrid = ReadFromBinaryFile<HexGrid>("HexField"); BinaryFormatter bf = new BinaryFormatter();
hexGrid.SpawnField(); FileStream file =
} File.Open(Application.persistentDataPath
} + $"/{fileName}.dat", FileMode.Open);
GridToSave data = (GridToSave)bf.Deserialize(file);
public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false) file.Close();
{
using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create)) if (fieldBaseGameObject != null)
{ {
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); DestroyImmediate(fieldBaseGameObject);
binaryFormatter.Serialize(stream, objectToWrite); }
if (gridCanvasInstance == null)
{
DestroyImmediate(gridCanvasInstance);
}
gridCanvasInstance = Instantiate(gridCanvas);
fieldBaseGameObject = new GameObject("HexField");
_height = data.height;
_width = data.width;
_cells = new HexCell[_width * _height];
foreach (var cell in data.cells)
{
if (cell == null)
continue;
CreateCell(cell.HexCoordinates.X, cell.HexCoordinates.Z, cell.index);
}
Debug.Log("Game data loaded!");
} }
else
Debug.LogError("There is no save data!");
} }
[EditorButton]
void SaveGrid(string fileName)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath
+ $"/{fileName}.dat");
GridToSave data = new GridToSave();
var tmp = new List<SerializibleHexCell>();
_cells.ToList().ForEach(cell => { tmp.Add(cell == null ? null : cell.ToSerializibleHexCell()); });
data.cells = tmp.ToArray();
data.width = _width;
data.height = _height;
bf.Serialize(file, data);
file.Close();
Debug.Log("Game data saved!");
}
public static T ReadFromBinaryFile<T>(string filePath) public static T ReadFromBinaryFile<T>(string filePath)
{ {
using (Stream stream = File.Open(filePath, FileMode.Open)) using (Stream stream = File.Open(filePath, FileMode.Open))
{ {
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); var binaryFormatter = new BinaryFormatter();
return (T)binaryFormatter.Deserialize(stream); return (T)binaryFormatter.Deserialize(stream);
} }
} }
@ -71,11 +153,83 @@ namespace DefaultNamespace
void HandleInput() void HandleInput()
{ {
Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition); Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(inputRay, out hit)) if (Physics.Raycast(inputRay, out var hit))
{ {
var coord = HexCoordinates.FromPosition(hit.point); var coord = HexCoordinates.FromPosition(hit.transform.position);
hexGrid.GetCellFromCoord(coord).PaintHex(UnitColor.Green); _cells.First(x => x.coordinates.Equals(coord)).gameObject.GetComponent<MeshRenderer>().material.color =
Color.green;
}
}
private void CreateCell(int x, int z, int i, bool isHexCoord = false)
{
Vector3 position;
var cellGO = Object.Instantiate(hexPrefab);
HexCell cell = _cells[i] = cellGO.AddComponent<HexCell>();
if (isHexCoord)
{
HexCoordinates coordinates = new HexCoordinates(x, z);
position = HexCoordinates.ToPosition(coordinates);
(x, z) = HexCoordinates.ToOffsetCoordinates(coordinates);
}
else
{
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z);
}
cell.transform.SetParent(fieldBaseGameObject.transform, false);
cell.transform.localPosition = position;
cell.index = i;
if (x > 0)
{
cell.SetNeighbor(HexDirection.W, _cells[i - 1]);
}
if (z > 0)
{
if ((z & 1) == 0)
{
cell.SetNeighbor(HexDirection.SE, _cells[i - _width]);
if (x > 0)
{
cell.SetNeighbor(HexDirection.SW, _cells[i - _width - 1]);
}
}
else
{
cell.SetNeighbor(HexDirection.SW, _cells[i - _width]);
if (x < _width - 1)
{
cell.SetNeighbor(HexDirection.SE, _cells[i - _width + 1]);
}
}
}
#if UNITY_EDITOR
TMP_Text label = Object.Instantiate(labelPrefab, gridCanvasInstance.transform, false);
label.rectTransform.anchoredPosition =
new Vector2(position.x, position.z);
label.text = cell.coordinates.ToStringOnSeparateLines();
#endif
}
private void SpawnField()
{
for (int z = 0, i = 0; z < _height; z++)
{
for (int x = 0; x < _width; x++)
{
CreateCell(x, z, i++);
}
} }
} }
} }

View File

@ -5,7 +5,8 @@ MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: defaultReferences:
- hexPrefab: {fileID: 1661242500252451528, guid: bcea5b4a96735bd4b936f8f3fefcc688, type: 3} - hexPrefab: {fileID: 1661242500252451528, guid: bcea5b4a96735bd4b936f8f3fefcc688, type: 3}
- labelPrefab: {fileID: 4726489279989878083, guid: f31e0880dd078104bb31dc0fd7ef9f19, type: 3} - labelPrefab: {fileID: 3836123284387241147, guid: efd47cbd22ddfee4aa2b1391914116fc, type: 3}
- gridCanvas: {fileID: 4726489279989878083, guid: f31e0880dd078104bb31dc0fd7ef9f19, type: 3}
executionOrder: 0 executionOrder: 0
icon: {instanceID: 0} icon: {instanceID: 0}
userData: userData:

View File

@ -13,6 +13,7 @@ namespace Items
public void Invoke(Action action) public void Invoke(Action action)
{ {
if(_action != null) return;
_action = action; _action = action;
OnItemUsed += _action; OnItemUsed += _action;
} }

View File

@ -0,0 +1,52 @@
using System;
using DefaultNamespace;
using HexFiled;
using UnityEngine;
using Weapons;
using Object = UnityEngine.Object;
namespace Items
{
[CreateAssetMenu(fileName = "SpecialWeapon", menuName = "Item/Special Weapon")]
public class SpecialWeapon : Item
{
[SerializeField] private Weapon _weapon;
[SerializeField] private GameObject _aimGameObject;
private GameObject _aimInstance;
private HexDirection _direction;
public void Invoke(Action action)
{
OnItemUsed ??= action;
if(_aimInstance == null || !_aimInstance.activeSelf)
_aimInstance = Object.Instantiate(_aimGameObject, Unit.Instance.transform);
_aimInstance.SetActive(false);
}
public void Aim(HexDirection direction)
{
_aimInstance.SetActive(true);
_aimInstance.transform.LookAt(HexManager.UnitCurrentCell[Unit.Color].cell
.GetNeighbor(direction).transform);
_direction = direction;
}
public void DeAim()
{
_aimInstance.SetActive(false);
}
public void Fire()
{
var cell = HexManager.UnitCurrentCell[Unit.Color].cell.GetNeighbor(_direction);
Unit.RotateUnit(new Vector2((cell.transform.position - Unit.Instance.transform.position).normalized.x,
(cell.transform.position - Unit.Instance.transform.position).normalized.z));
_aimInstance.SetActive(false);
var dir = DirectionHelper.DirectionTo(Unit.Instance.transform.position, cell.transform.position);
_weapon.Fire(Unit.Instance.transform, new Vector2(dir.x, dir.z));
OnItemUsed?.Invoke();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 30d4ee04b4d04d47ad8f932a004c1728
timeCreated: 1644227001

View File

@ -85,6 +85,9 @@ namespace Chars
case CaptureAbility ability: case CaptureAbility ability:
ability.UseAbility(); ability.UseAbility();
break; break;
case SpecialWeapon weapon:
weapon.Fire();
break;
} }
} }
@ -151,10 +154,12 @@ namespace Chars
ability.DeAim(); ability.DeAim();
return; return;
} }
ability.Aim(DirectionHelper.VectorToDirection(placeDir.normalized)); ability.Aim(DirectionHelper.VectorToDirection(placeDir.normalized));
_aimCount = 1; _aimCount = 1;
break; break;
case SpecialWeapon weapon:
weapon.Aim(DirectionHelper.VectorToDirection(placeDir.normalized));
break;
} }
} }
@ -179,6 +184,9 @@ namespace Chars
case Building building: case Building building:
_unitView.AimCanvas.SetActive(false); _unitView.AimCanvas.SetActive(false);
break; break;
case SpecialWeapon weapon:
weapon.DeAim();
break;
} }
} }