Skip to content

Commit

Permalink
Ataque e chave
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre-Messias committed Sep 30, 2024
1 parent 75c94af commit 0069843
Show file tree
Hide file tree
Showing 18 changed files with 536 additions and 74 deletions.
8 changes: 8 additions & 0 deletions Assets/Items.meta

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

12 changes: 12 additions & 0 deletions Assets/Items/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour
{
[Header("Status")]
public int damage = 1;
public float range = 1f;
public float attackCooldown = 1f;

}
11 changes: 11 additions & 0 deletions Assets/Items/Item.cs.meta

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

29 changes: 29 additions & 0 deletions Assets/Items/ScriptableItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Mantega;
using UnityEngine;

[CreateAssetMenu(fileName = "Item", menuName = "ScriptableObjects/Item", order = 1)]
public class ScriptableItem : ScriptableObject
{
public new string name;
public Sprite icon;

[Header("Prefab")]
public GameObject prefab;
public Item item;

public void Instantiate(Vector3 position, Quaternion rotation)
{
Instantiate(prefab, position, rotation);
}

private void OnValidate()
{
if(item == null && prefab != null)
{
if(!Generics.FamilyTryGetComponent(prefab, out item))
{
Debug.LogAssertion("Prefab must have a Item component");
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Items/ScriptableItem.cs.meta

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

18 changes: 18 additions & 0 deletions Assets/Items/Sword.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%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: d12505b8e17acbf44912c0220535c4d5, type: 3}
m_Name: Sword
m_EditorClassIdentifier:
name: Sword
icon: {fileID: 0}
prefab: {fileID: 6741968925869854149, guid: 10542f4c6e8b12f4baf1083d2b1dc4ab, type: 3}
item: {fileID: 7971206292936052240, guid: 10542f4c6e8b12f4baf1083d2b1dc4ab, type: 3}
8 changes: 8 additions & 0 deletions Assets/Items/Sword.asset.meta

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

49 changes: 49 additions & 0 deletions Assets/Items/Sword.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &6741968925869854149
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3863224172626366125}
- component: {fileID: 7971206292936052240}
m_Layer: 0
m_Name: Sword
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &3863224172626366125
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6741968925869854149}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &7971206292936052240
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6741968925869854149}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2818e2268277df0499785e7edfc40dcf, type: 3}
m_Name:
m_EditorClassIdentifier:
damage: 1
range: 1
attackCooldown: 1
7 changes: 7 additions & 0 deletions Assets/Items/Sword.prefab.meta

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

55 changes: 49 additions & 6 deletions Assets/PlayerAttack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
using Mantega;
using System;

public class PlayerAttack : MonoBehaviour
{
Expand All @@ -10,32 +11,74 @@ public class PlayerAttack : MonoBehaviour

[Header("Attack")]
[SerializeField] private Transform attackPoint;
[SerializeField] private float attackRange = 1f;
[SerializeField] private bool _canAttack = true;
public Action<float> attack;

[Header("Weapon")]
public ScriptableItem weapon;
[SerializeField] private GameObject _weaponGameObject;
[SerializeField] private Item _weaponItem;

void Start()
{
if (playerInput == null)
Generics.ReallyTryGetComponent(gameObject, out playerInput);
playerInput.attack += Attack;

if (weapon != null)
ActiveWeapon(weapon);
}

public void ResetAttack()
{
DesactiveWeapon();
_canAttack = true;
}

public void DesactiveWeapon()
{
if(_weaponGameObject != null)
Destroy(_weaponGameObject);
weapon = null;
_weaponItem = null;
}

public void ActiveWeapon(ScriptableItem newWeapon)
{
DesactiveWeapon();
weapon = newWeapon;
if(weapon.prefab != null)
{
_weaponGameObject = Instantiate(weapon.prefab, transform);
Generics.FamilyTryGetComponent(_weaponGameObject, out _weaponItem);
}
}

void Attack()
{
Debug.Log("Attack");
var collisions = Physics.OverlapSphere(attackPoint.position, attackRange);
if(!_canAttack || _weaponItem == null)
return;

_canAttack = false;
Invoke(nameof(CooldownAttack), _weaponItem.attackCooldown);
attack?.Invoke(_weaponItem.attackCooldown);

var collisions = Physics.OverlapSphere(attackPoint.position, _weaponItem.range);
foreach (var collision in collisions)
{
if (Generics.FamilyTryGetComponent(collision.gameObject, out HPController hpController) && hpController.gameObject.tag == "Enemy")
hpController.LoseHP(1);
hpController.LoseHP(_weaponItem.damage);
}
}

void CooldownAttack() => _canAttack = true;

private void OnDrawGizmosSelected()
{
if (attackPoint == null)
if (attackPoint == null || _weaponItem == null)
return;

Gizmos.DrawWireSphere(attackPoint.position, attackRange);
Gizmos.DrawSphere(attackPoint.position, _weaponItem.range);
}

void OnDestroy()
Expand Down
23 changes: 22 additions & 1 deletion Assets/Prefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ GameObject:
- component: {fileID: 9187463006278666535}
- component: {fileID: 2048224215761797906}
- component: {fileID: 6755525692779146323}
- component: {fileID: 669423193226920414}
m_Layer: 0
m_Name: Player
m_TagString: Player
Expand Down Expand Up @@ -385,7 +386,27 @@ MonoBehaviour:
m_EditorClassIdentifier:
playerInput: {fileID: 0}
attackPoint: {fileID: 1047143529578504164}
attackRange: 0.6
_canAttack: 1
weapon: {fileID: 11400000, guid: b73319ff41949104fb68b2ba76e1a4ef, type: 2}
_weaponGameObject: {fileID: 0}
_weaponItem: {fileID: 0}
--- !u!114 &669423193226920414
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4632741985066660338}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7ec79ce7ce2f0aa45abbbd8c4fc8acca, type: 3}
m_Name:
m_EditorClassIdentifier:
keys: 0
playerAttack: {fileID: 0}
playerMovement: {fileID: 0}
playerHealth: {fileID: 0}
basicWeapon: {fileID: 11400000, guid: b73319ff41949104fb68b2ba76e1a4ef, type: 2}
--- !u!1 &8088374784937446622
GameObject:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit 0069843

Please sign in to comment.