Organization of Game Files

Created folder for C# files, player, and enemy so it's a little cleaner...
This commit is contained in:
Iazubi
2024-10-23 17:28:44 -07:00
parent 9494eb1e34
commit 0a4d0e9c86
47 changed files with 32 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
using System.Collections.Generic;
using UnityEngine;
public class BackgroundTiler : MonoBehaviour
{
public GameObject tilePrefab; // The background tile prefab
public Transform player; // The player object to track
public int tileSize = 10; // Size of each tile (assuming square tiles)
public int renderDistance = 3; // Number of tiles to render around the player
private Vector2Int previousTile; // Tracks the last tile position
private Dictionary<Vector2Int, GameObject> activeTiles = new Dictionary<Vector2Int, GameObject>();
void Start()
{
UpdateTiles(); // Generate initial tiles
}
void Update()
{
Vector2Int currentTile = GetTilePosition(player.position);
if (currentTile != previousTile)
{
previousTile = currentTile;
UpdateTiles();
}
}
Vector2Int GetTilePosition(Vector3 position)
{
return new Vector2Int(Mathf.FloorToInt(position.x / tileSize), Mathf.FloorToInt(position.y / tileSize));
}
void UpdateTiles()
{
Vector2Int currentTile = GetTilePosition(player.position);
// Remove tiles that are out of range
List<Vector2Int> tilesToRemove = new List<Vector2Int>();
foreach (var tile in activeTiles)
{
if (Mathf.Abs(tile.Key.x - currentTile.x) > renderDistance || Mathf.Abs(tile.Key.y - currentTile.y) > renderDistance)
{
tilesToRemove.Add(tile.Key);
}
}
foreach (var tile in tilesToRemove)
{
Destroy(activeTiles[tile]);
activeTiles.Remove(tile);
}
// Add new tiles around the player
for (int x = -renderDistance; x <= renderDistance; x++)
{
for (int y = -renderDistance; y <= renderDistance; y++)
{
Vector2Int tilePos = new Vector2Int(currentTile.x + x, currentTile.y + y);
if (!activeTiles.ContainsKey(tilePos))
{
Vector3 tilePosition = new Vector3(tilePos.x * tileSize, tilePos.y * tileSize, 0);
GameObject newTile = Instantiate(tilePrefab, tilePosition, Quaternion.identity);
activeTiles.Add(tilePos, newTile);
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7e58a9223e11a452ebfa72dded313f23

View File

@@ -0,0 +1,49 @@
using UnityEngine;
public class GameState : MonoBehaviour
{
public State state;
public uint wave;
public GameObject ratbot;
public GameObject shop;
void Start() {
OpenShop();
}
public enum State {
Wave,
Shop,
Menu,
Paused,
}
// Update is called once per frame
void Update()
{
if (GameObject.FindGameObjectsWithTag("Ratbot").Length == 0 && state == State.Wave) {
OpenShop();
}
}
void OpenShop() {
state = State.Shop;
Instantiate(shop);
}
public void NewWave() {
// Close the shop
Destroy(GameObject.FindGameObjectWithTag("Shop"));
// Spawn ratbots around the player
// Amount, health, and damage scale with waves
state = State.Wave;
Vector3 playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
for (uint _ = 0; _ <= wave; _++) {
GameObject newRatbot = Instantiate(
ratbot,
Random.insideUnitCircle.normalized*20,
Quaternion.identity
);
newRatbot.GetComponent<Ratbot>().damageStrength = 5+wave*5;
newRatbot.GetComponent<Ratbot>().health = 5+wave*2;
}
wave++;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 58ed146781c2cc8bd972920fd08533fe

View File

@@ -0,0 +1,41 @@
using UnityEngine;
public class Ratbot : MonoBehaviour
{
public float moveSpeed;
public float damageStrength;
public float damageTimeoutSeconds;
public float health = 10;
float lastDamageTime;
GameState gameState;
GameObject player;
void Start() {
// Set gameState and player objects
player = GameObject.FindGameObjectWithTag("Player");
gameState = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameState>();
}
void Update() {
// Constantly move towards player
Rigidbody2D controller = GetComponent<Rigidbody2D>();
Vector2 direction = (player.transform.position - transform.position).normalized;
controller.AddRelativeForce(direction*moveSpeed*Time.deltaTime);
}
void OnCollisionStay2D(Collision2D collision) {
// Deal damage when colliding with the player on a 0.5 second cooldown
if (collision.gameObject.tag != "Player") return;
if (lastDamageTime > Time.fixedTime-damageTimeoutSeconds) return;
lastDamageTime = Time.fixedTime;
collision.gameObject.GetComponent<Teo>().health -= damageStrength;
}
// Die if health below zero
public void Damage(float hurtyAmount) {
health -= hurtyAmount;
if (health <= 0) {
Destroy(gameObject);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4d75aac518cf9dca0b16bb5a2e347204

View File

@@ -0,0 +1,51 @@
using UnityEngine;
public class ShopButton : MonoBehaviour
{
public UpgradeOption upgrade;
public float amount;
public enum UpgradeOption {
AttackRange,
AttackDistance,
AttackDamage,
AttackCooldown,
MaxHealth,
MovementSpeed,
DashDistance,
DashCooldown
}
void Start() {
GetComponent<UnityEngine.UI.Button>().onClick.AddListener(Upgrade);
}
void Upgrade() {
Teo player = GameObject.FindGameObjectWithTag("Player").GetComponent<Teo>();
switch (upgrade) {
case UpgradeOption.AttackRange:
player.swingRange += amount;
break;
case UpgradeOption.AttackDistance:
player.swingDistance += amount;
break;
case UpgradeOption.AttackDamage:
player.damageStrength += amount;
break;
case UpgradeOption.AttackCooldown:
player.attackCooldownSeconds += amount;
break;
case UpgradeOption.MaxHealth:
player.health += amount;
player.maxHealth += amount;
break;
case UpgradeOption.MovementSpeed:
player.moveSpeed += amount;
break;
case UpgradeOption.DashDistance:
player.dashDistance += amount;
break;
case UpgradeOption.DashCooldown:
player.dashCooldownSeconds += amount;
break;
}
GameObject.FindGameObjectWithTag("GameController").GetComponent<GameState>().NewWave();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a8661788ccc10d9fd9d1c9c2019bf2d3

View File

@@ -0,0 +1,24 @@
using UnityEngine;
public class Sword : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
// Point sword towards mouse cursor with garbage trig
transform.rotation = Quaternion.Euler(
new Vector3 (0, 0, Mathf.Atan2(
Screen.height/2 - Input.mousePosition.y,
Screen.width/2 - Input.mousePosition.x
) * Mathf.Rad2Deg + 135)
);
}
void Swing() {
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 12e2e0dada30fe215be589422e1df9b7

83
Assets/C# Scripts/Teo.cs Normal file
View File

@@ -0,0 +1,83 @@
using UnityEngine;
using System.Collections;
public class Teo : MonoBehaviour
{
public float health = 100;
public float maxHealth = 100;
public float moveSpeed = 10;
public float swingRange = 3;
public float swingDistance = 10;
public float damageStrength = 10;
public float attackCooldownSeconds = 10;
public float dashDistance = 5F;
public float dashCooldownSeconds = 5;
public float lastAttackTime = 0;
public float lastDashTime = 0;
public GameObject distanceIndicator;
Rigidbody2D controller;
GameState gameState;
bool dashing;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start() {
controller = GetComponent<Rigidbody2D>();
gameState = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameState>();
}
// Update is called once per frame
void Update() {
// Move player
if (!dashing) {
controller.AddRelativeForce (new Vector2 (
Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical")
).normalized * moveSpeed * Time.deltaTime);
}
// Attack on click if not on cooldown
if (Input.GetMouseButtonDown(0)) {
Attack();
lastAttackTime = Time.fixedTime;
}
if (Input.GetMouseButtonDown(1) && Time.fixedTime-lastDashTime > dashCooldownSeconds) {
StartCoroutine("Dash");
}
}
void Attack() {
if (Time.fixedTime-lastAttackTime < attackCooldownSeconds) {
return;
}
Instantiate(distanceIndicator, transform);
// Get all objects in range in the direction of cursor
RaycastHit2D[] hits = Physics2D.CircleCastAll(
transform.position,
swingRange,
new Vector2(
Input.mousePosition.x - Screen.width/2,
Input.mousePosition.y - Screen.height/2
).normalized,
swingDistance
);
// Damage all ratbots in scanned area
foreach (RaycastHit2D hit in hits) {
Ratbot ratbot = hit.collider.GetComponent<Ratbot>();
if (ratbot == null) continue;
ratbot.Damage(damageStrength);
}
}
IEnumerator Dash() {
BoxCollider2D collider = GetComponent<BoxCollider2D>();
lastDashTime = Time.fixedTime;
dashing = true;
collider.enabled = false;
controller.linearDamping = 0;
controller.linearVelocity = Vector2.zero;
controller.AddRelativeForce (new Vector2(
Input.mousePosition.x - Screen.width/2,
Input.mousePosition.y - Screen.height/2
).normalized * dashDistance);
yield return new WaitForSeconds(0.2F);
dashing = false;
collider.enabled = true;
controller.linearDamping = 15;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2f250cd978df15f8099e1f98a2e99ad1

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d3d8121a4c7ddf328a493dc0565428a6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
using UnityEngine;
public class AttackCooldown : MonoBehaviour
{
Teo player;
UnityEngine.UI.Image bar;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Teo>();
bar = GetComponent<UnityEngine.UI.Image>();
}
// Update is called once per frame
void Update()
{
GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Mathf.Max(0, 100-100*(Time.fixedTime-player.lastAttackTime)/player.attackCooldownSeconds));
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 44c4f0b9443ce51e7bb9fbb5e2ee3e47

View File

@@ -0,0 +1,26 @@
using UnityEngine;
using System.Collections;
public class DistanceIndicator : MonoBehaviour
{
public float multiplier;
Teo player;
SpriteRenderer sprite;
float timeCreated;
void Start() {
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Teo>();
sprite = GetComponent<SpriteRenderer>();
timeCreated = Time.fixedTime;
transform.localScale = new Vector3 (player.swingDistance*multiplier, player.swingDistance*multiplier, 1);
StartCoroutine("KillMyself");
}
void Update() {
sprite.color = new Color (0F, 0F, 0F, 0.5F-(Time.fixedTime-timeCreated));
}
IEnumerator KillMyself() {
yield return new WaitForSeconds (0.5F);
Destroy(gameObject);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 23dc3f32eba4c9e5aad24589fdbfa30c

View File

@@ -0,0 +1,17 @@
using UnityEngine;
public class HealthIndicator : MonoBehaviour
{
Teo player;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Teo>();
}
// Update is called once per frame
void Update()
{
GetComponent<TMPro.TMP_Text>().text = "Health: "+player.health+"/"+player.maxHealth;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d13803709cc4d9259a92b5dc9f49fe05

View File

@@ -0,0 +1,17 @@
using UnityEngine;
public class WaveIndicator : MonoBehaviour
{
GameState gameState;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
gameState = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameState>();
}
// Update is called once per frame
void Update()
{
GetComponent<TMPro.TMP_Text>().text = "Wave: "+gameState.wave;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 718783a6a2d0457d590a9bbe51b7d5e4