Skip to content

Commit

Permalink
added scripts data
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhmancs committed May 5, 2024
1 parent 19299e2 commit 491c50e
Show file tree
Hide file tree
Showing 13 changed files with 586 additions and 0 deletions.
13 changes: 13 additions & 0 deletions scripts/Billboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Billboard : MonoBehaviour
{
public Transform cam;

void LateUpdate()
{
transform.LookAt(transform.position + cam.forward);
}
}
39 changes: 39 additions & 0 deletions scripts/Enemy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
public Animator animator;
public int maxHealth = 100;
int currentHealth;

public HealthBar healthBar;

// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}

public void TakeDamage(int damage)
{
currentHealth -= damage;
animator.SetTrigger("Hurt");
healthBar.SetHealth(currentHealth);

if (currentHealth <= 0)
{
Die();
}
}

void Die()
{
animator.SetTrigger("IsDead");

GetComponent<Collider>().enabled = false;
this.enabled = false;
}
}
28 changes: 28 additions & 0 deletions scripts/HealthBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBar : MonoBehaviour
{

public Slider slider;
public Gradient gradient;
public Image fill;

public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;

fill.color = gradient.Evaluate(1f);
}

public void SetHealth(int health)
{
slider.value = health;

fill.color = gradient.Evaluate(slider.normalizedValue);
}

}
17 changes: 17 additions & 0 deletions scripts/MainMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}

public void QuitGame()
{
Application.Quit();
}
}
29 changes: 29 additions & 0 deletions scripts/MouseLook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;

float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 40f);

transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
35 changes: 35 additions & 0 deletions scripts/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

public int maxHealth = 100;
public int currentHealth;

public HealthBar healthBar;

// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
TakeDamage(20);
}
}

void TakeDamage(int damage)
{
currentHealth -= damage;

healthBar.SetHealth(currentHealth);
}
}
49 changes: 49 additions & 0 deletions scripts/PlayerCombat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCombat : MonoBehaviour
{
public Animator animator;
public Transform attackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public int attackDamage = 40;

public float attackRate = 2f;
float nextAttackTime = 0f;

// Update is called once per frame
void Update()
{
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.Q))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
}

void Attack()
{
animator.SetTrigger("Attack");

Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);

foreach (Collider enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}

void OnDrawGizmosSelected()
{
if (attackPoint == null)
{
return;
}
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
87 changes: 87 additions & 0 deletions scripts/PlayerMovement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public Animator animator;

public float speed = 12f;
public float gravity = -9.81f;

public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

public AudioClip footstepSound;
public AudioClip jumpingSound;

private AudioSource audioSource;

Vector3 velocity;
bool isGrounded;

// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
}

// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
animator.SetBool("IsJumping", false);
}

float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");

bool isMoving = Mathf.Abs(x) > 0.01f || Mathf.Abs(z) > 0.01f;
if (isMoving)
{
animator.SetBool("IsWalking", true);
PlayFootstepSound();
}
else
{
animator.SetBool("IsWalking", false);
animator.SetBool("IsIdle", true);
audioSource.Stop();
}

if (Input.anyKeyDown && (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A)
|| Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.E)))
{
animator.SetBool("IsWalking", true);
PlayFootstepSound();
}

Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);

if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
animator.SetBool("IsJumping", true);
audioSource.PlayOneShot(jumpingSound);
}

velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}

private void PlayFootstepSound()
{
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(footstepSound);
}
}
}
61 changes: 61 additions & 0 deletions scripts/ResumeMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ResumeMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
public GameObject stopMenuUI;
HealthBar healthBar = new HealthBar();

// Update is called once per frame
void Update()
{
if (healthBar.slider.value <= 0)
{
Stop();
}

if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
}
else
{
Pause();
}

}

}

public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}

public void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}

public void Stop()
{
stopMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}

public void Quit()
{
Application.Quit();
}
}
14 changes: 14 additions & 0 deletions scripts/Rotator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotator : MonoBehaviour
{
public float speed = 50f;

// Update is called once per frame
void Update()
{
transform.Rotate(0f, speed * Time.deltaTime, 0f);
}
}
Loading

0 comments on commit 491c50e

Please sign in to comment.