Skip to content
Snippets Groups Projects
Commit bf87a58a authored by Robin Halseth Sandvik's avatar Robin Halseth Sandvik
Browse files

Merge branch 'master' into 'main'

Ability rework.

See merge request !25
parents 3d9d074e 9413dd2b
No related branches found
No related tags found
2 merge requests!29Updating master,!25Ability rework.
......@@ -19,22 +19,22 @@ namespace BigSock {
/*
The name of the ability.
*/
public string Name { get; set; }
public abstract string Name { get;}
/*
The description of the ability.
*/
public string Description { get; set; }
public abstract string Description { get; }
/*
The id of the ability.
*/
public ulong Id { get; set; }
public abstract ulong Id { get; }
/*
The next time the ability has cooled down.
*/
public DateTime NextTimeCanUse { get; set; } = DateTime.Now;
public DateTime NextTimeCanUse { get; protected set; } = DateTime.Now;
/*
Whether the ability is ready.
......
......@@ -15,7 +15,7 @@ namespace BigSock {
/*
The attack stats of the ability.
*/
public IAttackStats AttackStats { get; set; }
public IAttackStats AttackStats { get; protected set; }
}
......
......@@ -16,17 +16,19 @@ namespace BigSock {
//protected static readonly GameObject PROJECTILE_BASE = new AttackMovement();
public const string PROJECTILE_NAME = "attack";
/*
The attack stats of the ability.
*/
public IAttackStats AttackStats { get; set; }
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 BasicProjectile1() {
Name = "Basic Player Projectile Attack";
Id = 101;
Description = "A basic projectile shooting attack the player has.";
AttackStats = new AttackStats{
Damage = 1f,
Knockback = 1f,
Range = 10f,
ProjectileSpeed = 10f,
AttackSpeed = 1f,
};
}
......@@ -38,8 +40,21 @@ namespace BigSock {
This should be overridden in sub-classes for the actual abilities.
*/
protected override bool Activate(Character actor, Vector3? target) {
if(target == null) return false;
var attack = (AttackStats) AttackStats.Apply(actor.Stats);
attack.Actor = actor;
var temp = (target.Value - actor.transform.position);
temp.z = 0;
var bullet = PrefabService.SINGLETON.Instance(PROJECTILE_NAME, actor.transform.position);
bullet.GetComponent<AttackMovement>().Actor = actor;
var bulletScript = bullet.GetComponent<AttackMovement>();
//bulletScript.Actor = actor;
bulletScript.Stats = attack;
bulletScript.Direction = temp.normalized;
//MonoBehaviour.Instantiate(PROJECTILE_BASE, (Vector3) actor.transform.position, PROJECTILE_BASE.transform.rotation);
return true;
......
......@@ -14,22 +14,27 @@ namespace BigSock {
/*
The damage of the attack.
*/
public float Damage { get; set; }
public float Damage { get; set; } = 1f;
/*
The knockback of the attack.
*/
public float Knockback { get; set; }
public float Knockback { get; set; } = 1f;
/*
The range of the attack.
*/
public float Range { get; set; }
public float Range { get; set; } = 1f;
/*
The projectile speed of the attack.
*/
public float ProjectileSpeed { get; set; } = 1f;
/*
The attack speed of the attack.
*/
public float AttackSpeed { get; set; }
public float AttackSpeed { get; set; } = 1f;
/*
The source of the attack.
......
......@@ -26,6 +26,11 @@ namespace BigSock {
*/
float Range { get; }
/*
The projectile speed of the attack.
*/
float ProjectileSpeed { get; }
/*
The attack speed of the attack.
*/
......@@ -41,4 +46,27 @@ namespace BigSock {
*/
Character Actor { get; }
}
/*
Holds extension methods for attack stat objects.
*/
public static class AttackStatsExtension {
/*
Applies the character's stats to the attack.
*/
public static IAttackStats Apply(this IAttackStats a, ICharacterStats b) {
return new AttackStats{
Damage = a.Damage * b.Damage,
Knockback = a.Knockback * b.Knockback,
Range = a.Range * b.Range,
ProjectileSpeed = a.ProjectileSpeed,
AttackSpeed = a.AttackSpeed * b.AttackSpeed,
Source = a.Source,
Actor = a.Actor,
};
}
}
}
\ No newline at end of file
......@@ -32,7 +32,7 @@ namespace BigSock {
protected IAttack _testAttack = new BasicProjectile1();
protected IAttack _testAttack;// = new BasicProjectile1();
public DateTime NextTimeCanAttack { get; private set; } = DateTime.Now;
......@@ -53,6 +53,7 @@ namespace BigSock {
TryPickUpItem(ItemService.SINGLETON.Get(201));
TryPickUpItem(ItemService.SINGLETON.Get(202));
var tmp = PrefabService.SINGLETON;
_testAttack = (IAttack) AbilityService.SINGLETON.Get(101);
}
......
using System.Collections;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.InputSystem;
using BigSock.Item;
namespace BigSock.Service {
/*
Service for handling abilities.
*/
public partial class AbilityService {
/*
The instance to use.
*/
public static readonly AbilityService SINGLETON = new AbilityService();
/*
Get an instance of the ability of the given id.
*/
public IAbility Get(ulong id) {
if(_abilities.TryGetValue(id, out var res)) return _new(res);
return null;
}
/*
Get a random ability from the ability pool.
*/
public IAbility GetRandom() {
var num = _rnd.Next(_abilityList.Count);
return _new(_abilityList[num]);
}
}
public partial class AbilityService {
private Dictionary<ulong, IAbility> _abilities = new Dictionary<ulong, IAbility>();
private List<IAbility> _abilityList = new List<IAbility>();
private System.Random _rnd = new System.Random();
private AbilityService() {
_loadItems();
}
/*
Load the abilities into the dictionary.
Reflection code: https://stackoverflow.com/a/6944605
*/
private void _loadItems() {
// Get the classs that inherit the item base class.
var types = Assembly
.GetAssembly(typeof(BaseAbility))
.GetTypes()
.Where(myType => myType.IsClass
&& !myType.IsAbstract
&& myType.IsSubclassOf(typeof(BaseAbility)));
// Create list of instances.
_abilityList = types
.Select(t => (IAbility) Activator.CreateInstance(t, new object[0]))
.ToList();
// Map to a dictionary by their ids.
_abilities = _abilityList
.ToDictionary(t => t.Id);
foreach(var a in _abilityList) {
Debug.Log($"[AbilityService._loadItems()] {a.Id}");
}
}
/*
Creates a new instance of the object.
*/
private IAbility _new(IAbility obj)
=> (IAbility) Activator.CreateInstance(obj.GetType(), new object[0]);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: accabe59812fa39448d6bd558041692d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -4,29 +4,44 @@ using UnityEngine;
using UnityEngine.InputSystem;
using static UnityEngine.GraphicsBuffer;
using BigSock.Service;
namespace BigSock {
public class AttackMovement : MonoBehaviour
{
public float speed = 10.0f;
bool moved = false;
Vector3 direction;
//Vector3 direction;
Vector3 startingPos;
/*
Damage of the character.
*/
public float Damage => baseDamage;
public float baseDamage = 1;
//public float Damage => baseDamage;
//public float baseDamage = 1;
public float KnockbackForce => knockbackForce;
public float knockbackForce = 1;
//public float KnockbackForce => knockbackForce;
//public float knockbackForce = 1;
/*
The character that activated the attack.
*/
public Character Actor { get; set; }
//public Character Actor { get; set; }
/*
The attack stats of the attack.
*/
public AttackStats Stats { get; set; }
/*
The direction of the attack.
*/
public Vector3 Direction { get; set; }
// Start is called before the first frame update
// Start is called before the first frame update
void Start()
{
}
......@@ -36,18 +51,26 @@ namespace BigSock {
{
if (!moved)
{
var mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var pos = transform.position;
var temp = (mouse - pos);
temp.z = 0;
direction = temp.normalized;
//var mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//var pos = transform.position;
//var temp = (mouse - pos);
//temp.z = 0;
//direction = temp.normalized;
//print($"[AttackMovement.Update()] {mouse} - {pos} = {direction}");
startingPos = transform.position;
moved = true;
}
//transform.Translate(new Vector3(1 * horizontalInput, 1 * verticalInput, 0) * speed * Time.deltaTime);
transform.Translate(direction * speed * Time.deltaTime);
transform.Translate(Direction * Stats.ProjectileSpeed * Time.deltaTime);
var traveled = Vector2.Distance(transform.position, startingPos);
// If we traveled passed our range: destroy this obj.
if(traveled > Stats.Range) {
PrefabService.SINGLETON.Destroy(gameObject);
}
}
......@@ -58,23 +81,24 @@ namespace BigSock {
if(target != null) {
var attack = new AttackStats{
Damage = Damage,
Knockback = KnockbackForce,
//Range = 0,
//AttackSpeed = AttackSpeed,
Source = transform.position,
Actor = Actor,
};
//var attack = new AttackStats{
// Damage = Damage,
// Knockback = KnockbackForce,
// //Range = 0,
// //AttackSpeed = AttackSpeed,
// Source = transform.position,
// Actor = Actor,
//};
Stats.Source = transform.position;
// If we have an actor: Apply their stat mods & trigger their OnHit.
if(Actor != null) {
attack.Damage *= Actor.Damage;
attack.Knockback *= Actor.KnockbackForce;
//Actor.TargetHit(target, attack);
}
//if(Actor != null) {
// attack.Damage *= Actor.Damage;
// attack.Knockback *= Actor.KnockbackForce;
// //Actor.TargetHit(target, attack);
//}
if(target.TakeDamage(attack)) {
if(target.TakeDamage(Stats)) {
}
}
......
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