Skip to content
Open
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
8 changes: 0 additions & 8 deletions UnityClient/Assets/MAIN.meta

This file was deleted.

58 changes: 58 additions & 0 deletions UnityClient/Assets/Persistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;

using UnityEngine;

public class Persistance
{
private static string savefile = "/savefile.ini";

public static void Save(string key, string value)
{
if (key.IndexOf(":") != -1) return;

string filename = Application.persistentDataPath + savefile;
string line = $"{key}:{value}";
bool found = false;

List<string> lines;
try {
using (StreamReader sr = new StreamReader(filename))
{
lines = new List<string>(
sr.ReadToEnd().Split(
new string[] { "\r\n", "\n" },
StringSplitOptions.None
)
);

for (int i = 0; i < lines.Count; ++i) {
if (!lines[i].StartsWith(key)) continue;

lines[i] = line;
found = true;
break;
}

if (!found)
lines.Add(line);
}
} catch (FileNotFoundException) {
lines = new List<string> { line };
}

using (StreamWriter sw = new StreamWriter(filename))
sw.Write(string.Join("\n", lines));
}
public static string Load(string key)
{
if (key.IndexOf(":") != -1) return null;

foreach (string line in File.ReadLines(Application.persistentDataPath + savefile))
if (line.StartsWith(key))
return line.Substring(key.Length + 1);

return null;
}
}
11 changes: 11 additions & 0 deletions UnityClient/Assets/Persistance.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions UnityClient/Assets/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using UnityEngine;

/// VERY basic player controller/logic
public class PlayerController : MonoBehaviour
{
private PlayerProperties properties = new PlayerProperties();

private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;

public bool twoPointFiveD = true; /// 2 & 2.5D

private const float playerSpeed = 10.0f;
private const float jumpHeight = 3.0f;
private const float gravityValue = -9.81f * 3;
private const float offScreenY = -10.0f;

private void CheckLoadSave()
{
/// prevent load/save if not grounded
if (!groundedPlayer) return;

if (Input.GetButtonDown("Save"))
properties.Save(this);

else if (Input.GetButtonDown("Load"))
properties.Load(this);
}

private void UseEnergy()
{
if (properties.decreaseEnergy())
if (properties.decreaseLifePoints())
Lose();
}
private void Lose()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
private bool IsOffScreen()
{
if (gameObject.transform.position.y < offScreenY)
return true;

// todo? add check on X axis

return false;
}

void Update()
{
groundedPlayer = controller.isGrounded;

if (groundedPlayer && playerVelocity.y < 0)
playerVelocity.y = 0f;

float z = 0.0f;
if (!twoPointFiveD) /// 3D
z = Input.GetAxis("Vertical");

Vector3 move = new Vector3(
Input.GetAxis("Horizontal"), 0, z
);

controller.Move(move * Time.deltaTime * playerSpeed);

if (move != Vector3.zero)
gameObject.transform.forward = move;

if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(
jumpHeight * -3.0f * gravityValue
);

UseEnergy();
}

playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);

/// VERY basic loss condition
if (IsOffScreen())
Lose();

CheckLoadSave();
}

private void Start()
{
controller =
gameObject.GetComponent<CharacterController>() ??
gameObject.AddComponent<CharacterController>()
;
}
}
11 changes: 11 additions & 0 deletions UnityClient/Assets/PlayerController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions UnityClient/Assets/PlayerProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;

using UnityEngine;

[Serializable]
public class PlayerProperties : ISerializationCallbackReceiver
{
#region gameplay
private const int energyFull = 2; // 10;

[SerializeField]
private int energy = energyFull;
// public int Energy() => energy;

[SerializeField]
private int lifePoints = 2; // 10
// public int LifePoints() => lifePoints;

public bool decreaseEnergy() => (--energy) <= 0;
public bool decreaseLifePoints()
{
energy = energyFull;
return (--lifePoints) <= 0;
}
#endregion

#region serialization_version
private Transform transform;

[SerializeField]
private Vector3 position;
[SerializeField]
private Quaternion rotation;

public void Save(PlayerController p)
{
transform = p.transform;

Persistance.Save(
"PlayerController",
JsonUtility.ToJson(this)
);
}
public void Load(PlayerController c)
{
string json = Persistance.Load("PlayerController");
if (json == null) return;

PlayerProperties p = JsonUtility.FromJson<PlayerProperties>(json);

c.transform.position = p.position;
c.transform.rotation = p.rotation;
this.energy = p.energy;
this.lifePoints = p.lifePoints;
}

public void OnAfterDeserialize() {}
public void OnBeforeSerialize()
{
if (transform == null) return;

position = transform.position;
rotation = transform.rotation;
}
#endregion
}
11 changes: 11 additions & 0 deletions UnityClient/Assets/PlayerProperties.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading