Merge pull request #8 from ProtoSharkk/stab

Rework attacking
This commit is contained in:
Iazubi
2024-10-27 00:30:05 -07:00
committed by GitHub
6 changed files with 35 additions and 20 deletions

View File

@@ -29,7 +29,7 @@ Transform:
m_GameObject: {fileID: 398740452138961375}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -11.37, y: -2.4779136, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.45, y: 0.45, z: 0.45}
m_ConstrainProportionsScale: 1
m_Children: []

View File

@@ -100,4 +100,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 23dc3f32eba4c9e5aad24589fdbfa30c, type: 3}
m_Name:
m_EditorClassIdentifier:
multiplier: 2.8
multiplier: 1

View File

@@ -106,8 +106,8 @@ MonoBehaviour:
health: 100
maxHealth: 100
moveSpeed: 15
swingRange: 2
swingDistance: 10
swingRangeDeg: 3
swingDistance: 2
damageStrength: 10
attackCooldownSeconds: 2
dashDistance: 20

View File

@@ -325,7 +325,11 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 6118034672792182209, guid: e05928a0d673caf999d31b31b994a112, type: 3}
propertyPath: swingDistance
value: 2
value: 3
objectReference: {fileID: 0}
- target: {fileID: 6118034672792182209, guid: e05928a0d673caf999d31b31b994a112, type: 3}
propertyPath: swingRangeDeg
value: 10
objectReference: {fileID: 0}
- target: {fileID: 6118034672792182209, guid: e05928a0d673caf999d31b31b994a112, type: 3}
propertyPath: distanceIndicator

View File

@@ -33,6 +33,7 @@ public class Ratbot : MonoBehaviour
// Die if health below zero
public void Damage(float hurtyAmount) {
Debug.Log(hurtyAmount);
health -= hurtyAmount;
if (health <= 0) {
Destroy(gameObject);

View File

@@ -43,9 +43,6 @@ public class Teo : MonoBehaviour
}
}
void Attack() {
if (Time.fixedTime-lastAttackTime < attackCooldownSeconds) {
return;
}
Instantiate(distanceIndicator, transform);
// Get all objects within swingDistance of player
Collider2D[] hits = Physics2D.OverlapCircleAll(
@@ -55,36 +52,49 @@ public class Teo : MonoBehaviour
// Damage all ratbots in scanned area
foreach (Collider2D hit in hits) {
Ratbot ratbot = hit.GetComponent<Ratbot>();
// If ratbot exists and is within swing range, damage it.
if (ratbot == null) continue;
// Find the closest point on the ratbot
Vector2 hitClosest = Physics2D.ClosestPoint(
transform.position, hit
);
// If ratbot's closest point within swing range, damage it.
// Calculate absolute difference between angles of ratbot and cursor
/* if (ratbot == null || Mathf.Abs(Mathf.Atan2(
ratbot.transform.position.y - transform.position.y,
ratbot.transform.position.x - transform.position.x
if (Mathf.Abs(Mathf.Atan2(
hitClosest.y - transform.position.y,
hitClosest.x - transform.position.x
) - Mathf.Atan2(
Screen.height/2 - Input.mousePosition.y,
Screen.width/2 - Input.mousePosition.x
Input.mousePosition.y - Screen.height/2,
Input.mousePosition.x - Screen.width/2
)) > swingRangeDeg * Mathf.Deg2Rad
) */
if (ratbot == null) {
continue;
}
ratbot.Damage(damageStrength);
) continue;
// Apply x^2 multiplier to damage if not fully charged
// Just use damageStrength if fully charged
float timeWaited = Time.fixedTime - lastAttackTime;
ratbot.Damage(
(timeWaited < attackCooldownSeconds)
? Mathf.Pow(timeWaited, 2) *
damageStrength /
Mathf.Pow(attackCooldownSeconds, 2)
: damageStrength
);
}
}
IEnumerator Dash() {
BoxCollider2D collider = GetComponent<BoxCollider2D>();
// Disable collision and linearDamping while dashing
lastDashTime = Time.fixedTime;
dashing = true;
collider.enabled = false;
controller.linearDamping = 0;
controller.linearVelocity = Vector2.zero;
// Apply a force to player towards mouse cursor
controller.AddRelativeForce (new Vector2(
Input.mousePosition.x - Screen.width/2,
Input.mousePosition.y - Screen.height/2
).normalized * dashDistance);
// Wait 0.2 seconds before ending the dash
yield return new WaitForSeconds(0.2F);
// Re enable collision and linearDamping after dash complete
dashing = false;
collider.enabled = true;
controller.linearDamping = 15;