Skip to content
Snippets Groups Projects
Commit 1f70411f authored by Robin Ruud Kristensen's avatar Robin Ruud Kristensen
Browse files
parents e8c1d0cf d53a19b8
No related branches found
No related tags found
Loading
Showing
with 253 additions and 67 deletions
......@@ -19,6 +19,7 @@ namespace BigSock {
public override ulong Id => 201;
public override string Name => "Dodge";
public override string Description => "A basic dodge move.";
public override string IconName => "item/foureyes";
public AbilityDodge() {
StaminaCost = 10;
......
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock {
/*
An object representing an ability held by a player.
*/
public class AbilityEntity {
// The ability held in this slot.
public IAbility Ability { get; }
// The index of the ability
public int Index { get; }
// The time this ability started charging.
public float ChargeStarted { get; set; }
// The keys bound to this ability.
public List<KeyCode> Keys { get; }
// Indicates whether or not the ability is charging.
public bool IsCharging => ChargeStarted > 0;
// The time this ability has been charging.
public float ChargeTime => Time.time - ChargeStarted;
// The percent of maximum time the ability has charged for.
public float ChargePercent => (Ability.FireType == FireType.Charge && IsCharging)
? Math.Clamp(ChargeTime / Ability.MaxCharge, 0, 1)
: 0;
// How far into the cooldown the attack is.
public float CooldownPercent
=> (float) Math.Clamp((DateTime.Now - Ability.NextTimeCanUse + Ability.Cooldown) / Ability.Cooldown, 0, 1);
public AbilityEntity(IAbility ability, int index, List<KeyCode> keys = null) {
if(ability == null) throw new ArgumentNullException(nameof(ability));
Ability = ability;
Index = index;
Keys = keys ?? new List<KeyCode>();
}
}
}
fileFormatVersion: 2
guid: bbd84b83cc8d3c6469bd9ac57d8ae37c
folderAsset: yes
DefaultImporter:
guid: b5734a7700fbf65438b9e4f94e46e5ce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -6,6 +6,8 @@ using System.Text;
using UnityEngine;
using UnityEngine.InputSystem;
using BigSock.Service;
namespace BigSock {
......@@ -33,6 +35,19 @@ namespace BigSock {
*/
public abstract ulong Id { get; }
/*
The icon of the ability.
*/
public Sprite Icon => SpriteService.SINGLETON.Get(IconName);
/*
The name of the icon this ability uses.
Override this to change what icon the item uses.
*/
public virtual string IconName { get; } = "item/runningshoes";
/*
The next time the ability has cooled down.
*/
......@@ -98,7 +113,7 @@ namespace BigSock {
if(par.ChargeTime < MinCharge) return false;
// Calculate how much optional charging we did.
par.ChargeTimePercent = Math.Clamp(par.ChargeTime - MinCharge / MaxCharge - MinCharge, 0f, 1f);
par.ChargeTimePercent = Math.Clamp((par.ChargeTime - MinCharge) / (MaxCharge - MinCharge), 0f, 1f);
}
// Check that the ability is cooled down.
......
......@@ -31,6 +31,11 @@ namespace BigSock {
*/
ulong Id { get; }
/*
The icon of the ability.
*/
Sprite Icon { get; }
/*
The next time the ability has cooled down.
*/
......
......@@ -23,6 +23,7 @@ namespace BigSock {
public override ulong Id => 101;
public override string Name => "Basic Player Projectile Attack";
public override string Description => "A basic projectile shooting attack the player has.";
public override string IconName => "item/coffee";
public BasicProjectile1() {
AttackStats = new AttackStats{
......
......@@ -23,6 +23,7 @@ namespace BigSock {
public override ulong Id => 104;
public override string Name => "Chargeable Player Attack";
public override string Description => "A basic projectile shooting attack the player has.";
public override string IconName => "item/elixirofspeed";
......
......@@ -20,6 +20,7 @@ namespace BigSock {
public override ulong Id => 102;
public override string Name => "Big Ball";
public override string Description => "It's big and slow, but it packs a punch.";
public override string IconName => "item/premature";
public BiggerSlowerProjectile() {
AttackStats = new AttackStats{
......
......@@ -20,6 +20,7 @@ namespace BigSock {
public const int SKILL_POINTS_START = 0; // Skill points to start with.
public const float XP_SCALE_RATE = 3.0f; // Multiplier for xp gain, helps test system while game is tiny.
public const int ABILITY_COUNT = 4; // Number of abilities.
public UtilBar utilBar;
......@@ -37,13 +38,25 @@ namespace BigSock {
bool canMove = true;
// The UI component for displaying abilities.
private AbilityCluster AbilityUI;
// The abilities of the player.
private List<AbilityEntity> _abilities = new List<AbilityEntity>();
protected IAttack _testAttack;
protected IAttack _testAttack2;
protected IAttack _testAttack3;
protected IAbility _dodge;
// A list of the keycodes that correspond to each of the player's abilities.
private List<List<KeyCode>> _keyMapping = new List<List<KeyCode>> {
new List<KeyCode>{KeyCode.Space, KeyCode.Mouse0},
new List<KeyCode>{KeyCode.Z},
new List<KeyCode>{KeyCode.C},
new List<KeyCode>{KeyCode.LeftShift},
};
//protected IAttack _testAttack;
//protected IAttack _testAttack2;
//protected IAttack _testAttack3;
//protected IAbility _dodge;
public DateTime NextTimeCanAttack { get; private set; } = DateTime.Now;
......@@ -56,6 +69,24 @@ namespace BigSock {
TryPickUpItem(ItemService.SINGLETON.Get(101));
}
/*
Updates the list of the user's abilities.
*/
protected void SetAbilities(List<IAbility> neo) {
var res = new List<AbilityEntity>();
for(int i = 0; i < neo.Count; ++i)
res.Add(new AbilityEntity(neo[i], i, _keyMapping[i]));
_abilities = res;
// Update the UI to reflect the new abilities.
AbilityUI?.SetElements(_abilities.Count);
foreach (var ability in _abilities) {
AbilityUI.SetAbility(ability.Index, ability);
}
}
// Start is called before the first frame update
protected override void Start()
{
......@@ -69,6 +100,7 @@ namespace BigSock {
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
AbilityUI = GameObject.Find("AbilityCluster")?.GetComponent<AbilityCluster>();
//!! DEBUG: Add item to player at start to test if it works.
......@@ -81,10 +113,20 @@ namespace BigSock {
//var tmp = PrefabService.SINGLETON;
//var tmp = SpriteService.SINGLETON;
_testAttack = (IAttack)AbilityService.SINGLETON.Get(104);
_testAttack2 = (IAttack)AbilityService.SINGLETON.Get(102);
_testAttack3 = (IAttack)AbilityService.SINGLETON.Get(101);
_dodge = AbilityService.SINGLETON.Get(201);
// Get the abilities and set them up.
var aService = AbilityService.SINGLETON;
var abilities = new List<IAbility> {
aService.Get(104),
aService.Get(102),
aService.Get(101),
aService.Get(201),
};
SetAbilities(abilities);
//_testAttack = (IAttack)AbilityService.SINGLETON.Get(104);
//_testAttack2 = (IAttack)AbilityService.SINGLETON.Get(102);
//_testAttack3 = (IAttack)AbilityService.SINGLETON.Get(101);
//_dodge = AbilityService.SINGLETON.Get(201);
_ = AudioService.SINGLETON;
_ = SpriteService.SINGLETON;
......@@ -131,44 +173,72 @@ namespace BigSock {
}
// Dictionary that holds start times for charging abilities.
Dictionary<KeyCode, float> chargeStarts = new Dictionary<KeyCode, float>();
//Dictionary<KeyCode, float> chargeStarts = new Dictionary<KeyCode, float>();
/*
Triggers an ability if it should be.
Need to add support for mouse buttons n shiet
*/
private void CheckAbilityInput(KeyCode key, IAbility ability) {
private void CheckAbilityInput(AbilityEntity ability) {
var par = GetAbilityParam(Camera.main.ScreenToWorldPoint(Input.mousePosition));
switch(ability.FireType) {
// Check input on each key, stop if one was a success.
foreach(var key in ability.Keys)
if(CheckAbilityInput(key, ability)) break;
// Update the UI.
AbilityUI?.SetCharge(ability.Index, ability.ChargePercent);
AbilityUI?.SetCooldown(ability.Index, 1f - ability.CooldownPercent);
}
private bool CheckAbilityInput(KeyCode key, AbilityEntity ability) {
var par = GetAbilityParam(Camera.main.ScreenToWorldPoint(Input.mousePosition));
switch (ability.Ability.FireType) {
// Standard: Press to fire.
case FireType.Standard:
if(Input.GetKeyDown(key)) ability.Use(par);
if (Input.GetKeyDown(key)) {
ability.Ability.Use(par);
return true;
}
break;
// FullAuto: Keep firing while key is down.
case FireType.FullAuto:
if(Input.GetKey(key)) ability.Use(par);
if (Input.GetKey(key)) {
ability.Ability.Use(par);
return true;
}
break;
// Charge: Fire when let go.
case FireType.Charge:
// If pressed down: Store start time.
if(Input.GetKeyDown(key)) chargeStarts[key] = Time.time;
if(Input.GetKeyDown(key)) {
// Only start charging if the ability is ready.
if(ability.Ability.Ready) ability.ChargeStarted = Time.time;
return true;
}
// If let go: Activate
else if(Input.GetKeyUp(key)) {
par.ChargeTime = Time.time - chargeStarts[key];
var t = ability.Use(par);
if(!t) {
if(par.ChargeTime < ability.MinCharge)
Debug.Log($"[PlayerController.CheckAbilityInput({key})] {ability.Name} not fired ({par.ChargeTime:N3} < {ability.MinCharge:N3})");
if(ability.IsCharging) {
par.ChargeTime = ability.ChargeTime;
var t = ability.Ability.Use(par);
if (!t) {
if (par.ChargeTime < ability.Ability.MinCharge)
Debug.Log($"[PlayerController.CheckAbilityInput({key})] {ability.Ability.Name} not fired ({par.ChargeTime:N3} < {ability.Ability.MinCharge:N3})");
}
ability.ChargeStarted = 0f; // Reset charge time when we release the key.
}
return true;
}
break;
default:
break;
}
return false;
}
private void Update()
{
// Regenerate mana & stamina.
......@@ -187,14 +257,18 @@ namespace BigSock {
// _testAttack.Use(par);
//}
// Check and update all abilities.
foreach(var ability in _abilities) {
CheckAbilityInput(ability);
}
// Check ability 1.
CheckAbilityInput(KeyCode.Space, _testAttack);
CheckAbilityInput(KeyCode.Mouse0, _testAttack);
//CheckAbilityInput(KeyCode.Space, _testAttack);
//CheckAbilityInput(KeyCode.Mouse0, _testAttack);
// Check ability 2.
CheckAbilityInput(KeyCode.Z, _testAttack2);
//CheckAbilityInput(KeyCode.Z, _testAttack2);
// Check ability 3.
CheckAbilityInput(KeyCode.LeftShift, _dodge);
CheckAbilityInput(KeyCode.C, _testAttack3);
//CheckAbilityInput(KeyCode.LeftShift, _dodge);
//CheckAbilityInput(KeyCode.C, _testAttack3);
//if(Input.GetKeyDown(KeyCode.Z)) Debug.Log($"[PlayerController.Update()] Z was pressed.");
......@@ -246,6 +320,15 @@ namespace BigSock {
var controlScheme = PrefabService.SINGLETON.Instance("UI/ControlScheme", canvas.transform);
}
}
/* for testing ui
if (Input.GetKeyDown(KeyCode.P))
{
GameObject abil = GameObject.Find("AbilityCluster");
var abilScript = abil.GetComponent<AbilityCluster>();
abilScript.ResetElements();
}
*/
}
/*
......
......@@ -54,8 +54,8 @@ namespace BigSock.Service {
if(file.Contains(".meta")) continue;
var name = _sanitize(file.Replace(".wav", "").Replace(".mp3", "").Replace("Assets\\Resources\\sound\\", ""));
AudioClip go = Resources.Load<AudioClip>( file.Replace("Assets\\Resources\\", "").Replace(".wav", "").Replace(".mp3", "") );
Debug.Log($"[AudioService._loadItems()] {name}");
if(go == null) Debug.Log($"[AudioService._loadItems()] ITEM IS NULL!!! {name}");
//Debug.Log($"[AudioService._loadItems()] {name}");
//if(go == null) Debug.Log($"[AudioService._loadItems()] ITEM IS NULL!!! {name}");
dict[name] = go;
}
......
......@@ -12,23 +12,38 @@ namespace BigSock.UI
private List<AbilityElement> abilityList;
public void SetElements(int elementAmount)
{
{
abilityList = new List<AbilityElement>();
ResetElements();
for (int i = 0; i < elementAmount; ++i)
{
{
abilityList.Add(PrefabService.SINGLETON.Instance(ABILITYELEMENT, transform).GetComponent<AbilityElement>());
}
}
public void SetCooldown(int index, float amount)
//Clears all elements in ability cluster.
public void ResetElements()
{
abilityList.Clear();
var layout = this.GetComponent<HorizontalLayoutGroup>();
for (int i = 0; i < layout.transform.childCount; i++)
{
Destroy(layout.transform.GetChild(i).gameObject);
}
}
public void SetCooldown(int index, float amount)
{
abilityList[index].WithCooldown(amount);
}
}
public void SetCharge(int index, float amount)
{
abilityList[index].WithCharge(amount);
}
public void SetAbility(int index, AbilityEntity ability) {
abilityList[index].WithAbility(ability);
}
}
}
......@@ -6,39 +6,48 @@ using UnityEngine.UI;
namespace BigSock.UI
{
public class AbilityElement : MonoBehaviour
{
private Slider chargeSlider, cooldownSlider;
public class AbilityElement : MonoBehaviour
{
private Slider chargeSlider, cooldownSlider;
private Image _sprite;
public AbilityElement WithCharge(float? value = null, float? maxValue = null)
{
if (value != null) chargeSlider.value = value.Value;
public AbilityElement WithCharge(float? value = null, float? maxValue = null)
{
if (value != null) chargeSlider.value = value.Value;
if (maxValue != null) chargeSlider.maxValue = maxValue.Value;
return this;
}
if (maxValue != null) chargeSlider.maxValue = maxValue.Value;
return this;
}
public AbilityElement WithCooldown(float? value = null, float? maxValue = null)
{
if (value != null) cooldownSlider.value = value.Value;
public AbilityElement WithCooldown(float? value = null, float? maxValue = null)
{
if (value != null) cooldownSlider.value = value.Value;
if (maxValue != null) cooldownSlider.maxValue = maxValue.Value;
return this;
}
if (maxValue != null) cooldownSlider.maxValue = maxValue.Value;
return this;
}
public AbilityElement WithAbility(AbilityEntity ability) {
_sprite ??= transform.Find("Sprite").GetComponent<UnityEngine.UI.Image>();
// Start is called before the first frame update
void Start()
{
chargeSlider = transform.Find("ChargeSlider").GetComponent<Slider>();
cooldownSlider = transform.Find("ReloadSlider").GetComponent<Slider>();
}
if (_sprite != null)
_sprite.overrideSprite = ability.Ability.Icon ?? _sprite.overrideSprite;
return this;
}
// Update is called once per frame
void Update()
{
}
// Start is called before the first frame update
void Start() {
chargeSlider = transform.Find("ChargeSlider").GetComponent<Slider>();
cooldownSlider = transform.Find("ReloadSlider").GetComponent<Slider>();
_sprite ??= transform.Find("Sprite").GetComponent<UnityEngine.UI.Image>();
}
// Update is called once per frame
void Update()
{
}
}
}
fileFormatVersion: 2
guid: 4cc5587ab1cdd184ea4bff920c7f5f71
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -15,7 +15,7 @@ GameObject:
- component: {fileID: 532417261}
m_Layer: 11
m_Name: ClosedChest
m_TagString: Untagged
m_TagString: Chest
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment