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

Standardized the class hierarchy.

parent 3a82fa5d
Branches
No related tags found
1 merge request!5Master
Showing
with 503 additions and 402 deletions
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock {
// Common class for all characters.
public class Character : Entity {
/*
Attack speed of the character.
*/
public double AttackSpeed => baseAttackSpeed;
public double baseAttackSpeed = 1;
/*
Movement speed of the character.
*/
public double MovementSpeed => baseMovementSpeed;
public double baseMovementSpeed = 1;
/*
Damage of the character.
*/
public double Damage => baseDamage;
public double baseDamage = 1;
/*
Hit points of the character.
*/
public double HP {
get => baseHP;
set => baseHP = value;
}
public double baseHP = 10;
/*
Maximum hit points of the character.
*/
public double MaxHP => baseMaxHP;
public double baseMaxHP = 10;
}
}
fileFormatVersion: 2
guid: 227b56e9abdf92742969115a025426b5
guid: b60f5b1f7377c684ead118a2cc35234d
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
......@@ -2,6 +2,9 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BigSock {
public class DestroyObject : MonoBehaviour
{
......@@ -21,3 +24,4 @@ public class DestroyObject : MonoBehaviour
{
}
}
}
......@@ -3,6 +3,7 @@ using System.Collections.Generic;
using UnityEngine;
using System;
namespace BigSock {
public class EmptyCollider : MonoBehaviour
{
public event Action<Collider2D> OnColliderEnter2D_Action;
......@@ -26,3 +27,4 @@ public class EmptyCollider : MonoBehaviour
OnColliderExit2D_Action?.Invoke(other);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: b1aeda8fe8f5c1f4fa7a8f47e2864211
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock {
// Takes and handles input and movement for a player character
public class Entity : MonoBehaviour {
}
}
fileFormatVersion: 2
guid: 57ff668aaac9f3444ba1f1e9528716ea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -4,10 +4,12 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BigSock {
// Takes and handles input and movement for a player character
public class PlayerController : MonoBehaviour
public class PlayerController : Character
{
public float moveSpeed = 1f;
public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter;
......@@ -24,7 +26,7 @@ public class PlayerController : MonoBehaviour
public bool Alive { get; private set; } = true;
public double Hp { get; private set; } = 100;
//public double Hp { get; private set; } = 100;
public DateTime NextTimeCanTakeDamage { get; private set; } = DateTime.Now;
public TimeSpan IFrameDuration { get; private set; } = new TimeSpan(0, 0, 2);
......@@ -41,7 +43,7 @@ public class PlayerController : MonoBehaviour
Adds damage to the player if they don't have IFrames.
*/
public bool TakeDamage(double amount) {
print($"[PlayerController.TakeDamage()] start. | {Hp} - {amount}");
print($"[PlayerController.TakeDamage()] start. | {HP} - {amount}");
// Check if player has IFrames
if(NextTimeCanTakeDamage > DateTime.Now)
return false;
......@@ -49,7 +51,7 @@ public class PlayerController : MonoBehaviour
NextTimeCanTakeDamage = DateTime.Now + IFrameDuration;
// Add damage
Hp -= amount;
HP -= amount;
TryKill();
......@@ -60,8 +62,8 @@ public class PlayerController : MonoBehaviour
Try to kill the player.
*/
public bool TryKill() {
print($"[PlayerController.TryKill()] start. | {Hp}, {Alive}");
if(Alive && Hp <= 0) {
print($"[PlayerController.TryKill()] start. | {HP}, {Alive}");
if(Alive && HP <= 0) {
Alive = false;
LockMovement();
......@@ -123,10 +125,10 @@ public class PlayerController : MonoBehaviour
direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
castCollisions, // List of collisions to store the found collisions into after the Cast is finished
moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
(float) MovementSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
if(count == 0){
rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime);
return true;
} else {
return false;
......@@ -151,4 +153,4 @@ public class PlayerController : MonoBehaviour
canMove = true;
}
}
}
......@@ -4,6 +4,8 @@ using UnityEngine;
using UnityEngine.InputSystem;
using static UnityEngine.GraphicsBuffer;
namespace BigSock {
public class AttackMovement : MonoBehaviour
{
public float speed = 10.0f;
......@@ -53,3 +55,4 @@ public class AttackMovement : MonoBehaviour
}
}
}
}
\ No newline at end of file
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
namespace BigSock {
public partial class EnemyController : Character {
public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
private Transform target;
Animator m_Animator;
private float distance;
private float canAttack;
private EmptyCollider followCollider;
private EmptyCollider attackCollider;
private bool isInMelee = false;
Rigidbody2D rb;
// private Collider2D_Proxy secondCollider;
void Start(){
rb = GetComponent<Rigidbody2D>();
m_Animator = gameObject.GetComponent<Animator>();
followCollider = transform.Find("followCollider").GetComponent<EmptyCollider>();
followCollider.OnColliderEnter2D_Action += Move_OnColliderEnter2D;
followCollider.OnColliderStay2D_Action += Move_OnColliderStay2D;
followCollider.OnColliderExit2D_Action += Move_OnColliderExit2D;
attackCollider = transform.Find("MeleeCollider").GetComponent<EmptyCollider>();
attackCollider.OnColliderEnter2D_Action += Attack_OnColliderEnter2D;
attackCollider.OnColliderStay2D_Action += Attack_OnColliderStay2D;
attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D;
}
private void RotateAnimation(Vector2 direction)
{
if (direction.x > 0.01f){
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
else if (direction.x < -0.01f){
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
}
private void Update(){
if (target != null && !isInMelee){
/* //walk
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, target.position, step);
//distance = Vector3.Distance (transform.position, target.position);
//roter
RotateAnimation();
*/
TryMove((new Vector2(target.position.x, target.position.y) - rb.position).normalized);
}
else{
}
}
private bool TryMove(Vector2 direction) {
if(direction != Vector2.zero) {
// Check for potential collisions
int count = rb.Cast(
direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
castCollisions, // List of collisions to store the found collisions into after the Cast is finished
(float) MovementSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
//Debug.Log($"cast {string.Join(", ", castCollisions.Select(x => $"{x.collider.isTrigger}"))}");
//Debug.Log($"rb.position : {rb.position}");
//Debug.Log($"target.position : {target.position}");
//Debug.Log($"direction : {direction}");
if(count == 0){
rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime);
print($"rb.position {rb.position}");
print($"direction {direction}");
RotateAnimation(direction);
return true;
} else {
return false;
}
} else {
// Can't move if there's no direction to move in
return false;
}
}
}
public partial class EnemyController { //attack
private void Attack_OnColliderEnter2D(Collider2D other){
if (other.gameObject.tag == "Player")
isInMelee = true;
}
private void Attack_OnColliderStay2D(Collider2D other){
if (other.gameObject.tag == "Player"){
var player = other.gameObject.GetComponent<PlayerController>();
if(player.TakeDamage(Damage)){
//knockback ?
//animer nå ?
}
}
}
private void Attack_OnColliderExit2D(Collider2D other){
if (other.gameObject.tag == "Player")
isInMelee = false;
}
}
public partial class EnemyController { //move
private void Move_OnColliderEnter2D(Collider2D other)
{
Debug.Log("enter");
if (other.gameObject.tag == "Player"){
Debug.Log("enter if");
m_Animator.SetTrigger("walk");
target = other.transform;
}
}
private void Move_OnColliderStay2D(Collider2D other)
{
if (other.gameObject.tag == "Player"){
}
}
private void Move_OnColliderExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player"){
m_Animator.SetTrigger("idle");
target = other.transform;
target = null;
}
}
}
}
fileFormatVersion: 2
guid: 17f5c644f1bb62e49add8056d54c83ff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public partial class enemyMovement : MonoBehaviour{
public float speed = 0.6f;
public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
private Transform target;
Animator m_Animator;
private float distance;
public float attackSpeed = 1f;
private float canAttack;
private float baseDamage = 1f;
private EmptyCollider followCollider;
private EmptyCollider attackCollider;
private bool isInMelee = false;
Rigidbody2D rb;
// private Collider2D_Proxy secondCollider;
void Start(){
rb = GetComponent<Rigidbody2D>();
m_Animator = gameObject.GetComponent<Animator>();
followCollider = transform.Find("followCollider").GetComponent<EmptyCollider>();
followCollider.OnColliderEnter2D_Action += Move_OnColliderEnter2D;
followCollider.OnColliderStay2D_Action += Move_OnColliderStay2D;
followCollider.OnColliderExit2D_Action += Move_OnColliderExit2D;
attackCollider = transform.Find("MeleeCollider").GetComponent<EmptyCollider>();
attackCollider.OnColliderEnter2D_Action += Attack_OnColliderEnter2D;
attackCollider.OnColliderStay2D_Action += Attack_OnColliderStay2D;
attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D;
}
private void RotateAnimation(Vector2 direction)
{
if (direction.x > 0.01f){
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
else if (direction.x < -0.01f){
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
}
private void Update(){
if (target != null && !isInMelee){
/* //walk
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, target.position, step);
//distance = Vector3.Distance (transform.position, target.position);
//roter
RotateAnimation();
*/
TryMove((new Vector2(target.position.x, target.position.y) - rb.position).normalized);
}
else{
}
}
private bool TryMove(Vector2 direction) {
if(direction != Vector2.zero) {
// Check for potential collisions
int count = rb.Cast(
direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
castCollisions, // List of collisions to store the found collisions into after the Cast is finished
speed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
//Debug.Log($"cast {string.Join(", ", castCollisions.Select(x => $"{x.collider.isTrigger}"))}");
//Debug.Log($"rb.position : {rb.position}");
//Debug.Log($"target.position : {target.position}");
//Debug.Log($"direction : {direction}");
if(count == 0){
rb.MovePosition(rb.position + direction * speed * Time.fixedDeltaTime);
print($"rb.position {rb.position}");
print($"direction {direction}");
RotateAnimation(direction);
return true;
} else {
return false;
}
} else {
// Can't move if there's no direction to move in
return false;
}
}
}
public partial class enemyMovement { //attack
private void Attack_OnColliderEnter2D(Collider2D other){
if (other.gameObject.tag == "Player")
isInMelee = true;
}
private void Attack_OnColliderStay2D(Collider2D other){
if (other.gameObject.tag == "Player"){
var player = other.gameObject.GetComponent<PlayerController>();
if(player.TakeDamage(baseDamage)){
//knockback ?
//animer nå ?
}
}
}
private void Attack_OnColliderExit2D(Collider2D other){
if (other.gameObject.tag == "Player")
isInMelee = false;
}
}
public partial class enemyMovement { //move
private void Move_OnColliderEnter2D(Collider2D other)
{
Debug.Log("enter");
if (other.gameObject.tag == "Player"){
Debug.Log("enter if");
m_Animator.SetTrigger("walk");
target = other.transform;
}
}
private void Move_OnColliderStay2D(Collider2D other)
{
if (other.gameObject.tag == "Player"){
}
}
private void Move_OnColliderExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player"){
m_Animator.SetTrigger("idle");
target = other.transform;
target = null;
}
}
}
......@@ -140,14 +140,13 @@ MonoBehaviour:
m_GameObject: {fileID: 2996495149472241661}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 227b56e9abdf92742969115a025426b5, type: 3}
m_Script: {fileID: 11500000, guid: 17f5c644f1bb62e49add8056d54c83ff, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 0.6
collisionOffset: 0.05
movementFilter:
useTriggers: 0
useLayerMask: 1
useLayerMask: 0
useDepth: 0
useOutsideDepth: 0
useNormalAngle: 0
......@@ -159,7 +158,6 @@ MonoBehaviour:
maxDepth: 0
minNormalAngle: 0
maxNormalAngle: 0
attackSpeed: 3
--- !u!61 &2395291586284291126
BoxCollider2D:
m_ObjectHideFlags: 0
......@@ -244,7 +242,7 @@ MonoBehaviour:
m_GameObject: {fileID: 7539630614846898202}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 71e6597ff848be34e979f08b6041384a, type: 3}
m_Script: {fileID: 11500000, guid: b1aeda8fe8f5c1f4fa7a8f47e2864211, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8620845285361089561
......@@ -305,6 +303,6 @@ MonoBehaviour:
m_GameObject: {fileID: 8620845285361089561}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 71e6597ff848be34e979f08b6041384a, type: 3}
m_Script: {fileID: 11500000, guid: b1aeda8fe8f5c1f4fa7a8f47e2864211, type: 3}
m_Name:
m_EditorClassIdentifier:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment