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

Mouse pointer & attack speed.

- Projectiles follow mouse position.
- Player sprite follows mouse position.
- Cooldown on attacks.
- Can attack with left-click.
parent b9701362
No related branches found
No related tags found
1 merge request!8Mouse pointer & attack speed.
......@@ -13,6 +13,11 @@ namespace BigSock {
*/
public double AttackSpeed => baseAttackSpeed;
public double baseAttackSpeed = 1;
/*
Cooldown between attacks.
*/
public double AttackCooldown => 1.0 / AttackSpeed;
/*
Movement speed of the character.
......
......@@ -30,7 +30,12 @@ namespace BigSock {
public DateTime NextTimeCanTakeDamage { get; private set; } = DateTime.Now;
public TimeSpan IFrameDuration { get; private set; } = new TimeSpan(0, 0, 2);
// Start is called before the first frame update
public DateTime NextTimeCanAttack { get; private set; } = DateTime.Now;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
......@@ -94,9 +99,15 @@ namespace BigSock {
}
// Set direction of sprite to movement direction
if(movementInput.x < 0) {
var mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var pos = transform.position;
var temp = (mouse - pos);
//temp.z = 0;
//direction = temp.normalized;
if(temp.x < 0) {
spriteRenderer.flipX = true;
} else if (movementInput.x > 0) {
} else if (temp.x > 0) {
spriteRenderer.flipX = false;
}
}
......@@ -106,16 +117,22 @@ namespace BigSock {
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (movementInput.x < 0 )
{
Instantiate(attack, new Vector3(transform.position.x - 1.0f, transform.position.y, transform.position.z), attack.transform.rotation);
} else if (movementInput.x >= 0)
{
Instantiate(attack, new Vector3(transform.position.x + 1.0f, transform.position.y, transform.position.z), attack.transform.rotation);
}
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
// Manage attack cooldown.
if(NextTimeCanAttack <= DateTime.Now) {
NextTimeCanAttack = DateTime.Now.AddSeconds(AttackCooldown);
Instantiate(attack, new Vector3(transform.position.x, transform.position.y, transform.position.z), attack.transform.rotation);
//if(movementInput.x < 0 ) {
// Instantiate(attack, new Vector3(transform.position.x - 1.0f, transform.position.y, transform.position.z), attack.transform.rotation);
//} else if(movementInput.x >= 0) {
// Instantiate(attack, new Vector3(transform.position.x + 1.0f, transform.position.y, transform.position.z), attack.transform.rotation);
//}
}
}
}
private bool TryMove(Vector2 direction) {
......
......@@ -8,51 +8,32 @@ namespace BigSock {
public class AttackMovement : MonoBehaviour
{
public float speed = 10.0f;
bool moved = false;
float horizontalInput;
float verticalInput;
public float speed = 10.0f;
bool moved = false;
Vector3 direction;
// Start is called before the first frame update
void Start()
{
}
// Start is called before the first frame update
void Start()
// Update is called once per frame
void Update()
{
if (!moved)
{
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}");
moved = true;
}
// Update is called once per frame
void Update()
{
if (!moved)
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
moved = true;
}
if (horizontalInput != 0 && verticalInput != 0)
{
transform.Translate(new Vector3(1 * horizontalInput, 1 * verticalInput, 0) * speed * Time.deltaTime);
}
else if (horizontalInput < 0)
{
transform.Translate(-Vector3.right * speed * Time.deltaTime);
}
else if (horizontalInput > 0)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
else if (verticalInput > 0)
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
else if (verticalInput < 0)
{
transform.Translate(-Vector3.up * speed * Time.deltaTime);
}
else
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
//transform.Translate(new Vector3(1 * horizontalInput, 1 * verticalInput, 0) * speed * Time.deltaTime);
transform.Translate(direction * speed * Time.deltaTime);
}
}
}
\ No newline at end of file
......@@ -24,7 +24,7 @@ namespace BigSock {
Rigidbody2D rb;
// private Collider2D_Proxy secondCollider;
void Start(){
......@@ -89,8 +89,8 @@ namespace BigSock {
//Debug.Log($"direction : {direction}");
if(count == 0){
rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime);
print($"rb.position {rb.position}");
print($"direction {direction}");
//print($"rb.position {rb.position}");
//print($"direction {direction}");
RotateAnimation(direction);
return true;
......
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