top of page
Gradient Background
Gradient Background

ColorBomb

Genre : 2D , Strategy , Action

Platform : PC

Mode : Online Multi-Player

"Color Bomb" เป็นเกมแนว Online Multi-player, 2D, Strategy ที่ผู้เล่นจะได้รับบทเป็นผู้บังคับตัวละครทหารจากประเทศต่างๆ ที่มาแข่งขันระเบิดสีเพื่อยืดพื้นที่ในดาวดวงใหม่

using System;
using System.Collections;
using UnityEngine;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine.SceneManagement;

public class PlayerController : NetworkBehaviour
{
    
    [SerializeField] private float _moveSpeed = 5f;
    [SerializeField] private int teamNumber = 0;//0 : Yellow , 1 : Purple , 2 : Green , 3 : Blue

    private bool isSpawn = false;
    public  int distance = 2;

    //SpeedBoost
    private float normalSpeed;
    private float _itemDuration;
    
    //Protection
    private bool Protection ; 
    private float _protectDuration;
    
    //ExplosionRangeOne
    private bool ExplosionRangeOne;
    private float _explosionRangeOneDuration;
    
    //ExplosionRangeOne
    private bool ExplosionRangeTwo;
    
    //Prefab
    [SerializeField] private GameObject[] bombPrefab;

    [SerializeField] private Sound Dead , Collect , Placebomb ;

    //Grid
    private Grid grid;
    private GameObject _grid;

    private Rigidbody2D rb;

    private int maxBombs = 3;                   //Change Max Bomb HERE <--
    private float refillTime = 3f;             //Change Refill Bomb delay HERE <--

    private int currentBombs;
    private bool isRefilling;
    private bool dieing;

    private GameManager _gameManager;

    [SerializeField] private Animator animator;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        normalSpeed = _moveSpeed;
        currentBombs = maxBombs;
        isRefilling = false;
        ExplosionRangeOne = false;
        afterplay = false;
        dieing = false;
    }
 
    private void Update()
    {
        if(IsOwner) 
        {
            animator.SetInteger("teamNumber" , teamNumber);
            spawnpoint();
            if( GameObject.FindGameObjectWithTag("Grid") == null ) return;
             _grid = GameObject.FindGameObjectWithTag("Grid");
             grid = _grid.GetComponent<Grid>();
            Movement();
            GameManager.instance.BombCountText.text = /*"current Bomb : " + */"" +currentBombs;
        }
        
    }

    private void PlaceBomb()
    {
        if( currentBombs > 0)
        {
            SoundManager.instance.Play(Collect);
            Vector3Int cellPosition = grid.WorldToCell(transform.position);
            Vector3 cellCenter = grid.GetCellCenterWorld(cellPosition);
            SpawnBombServerRpc(cellCenter);
            currentBombs--;
            isRefilling = true;
            StartCoroutine(RefillBombs());
            if (ExplosionRangeOne)
            {
                SpawnBombOptionOneServerRpc(cellCenter);
            }
            SoundManager.instance.Play(Placebomb);
            animator.SetTrigger("Hold");

        }
    }

    IEnumerator RefillBombs()
    {
        yield return new WaitForSeconds(refillTime);
        currentBombs++;
        isRefilling = false;
    }

    [ClientRpc]
    void SpawnBombClientRpc( Vector3 cellCenter )
    {
        GameObject temp = bombPrefab[teamNumber];
        temp.GetComponentInChildren<Bomb>()._distance = distance ;
        Instantiate(temp , cellCenter , Quaternion.identity);
    }
    [ClientRpc]
    void SpawnBombOptionOneClientRpc( Vector3 cellCenter )
    {
        GameObject temp = bombPrefab[teamNumber];
        temp.GetComponentInChildren<Bomb>()._distance = distance + 1;
        Instantiate(temp , cellCenter , Quaternion.identity);
    }
    [ClientRpc]
    void SpawnBombOptionTwoClientRpc( Vector3 cellCenter )
    {
        GameObject temp = bombPrefab[teamNumber];
        temp.GetComponentInChildren<Bomb>()._distance = distance;
        Instantiate(temp , cellCenter , Quaternion.identity);
    }

    [ServerRpc]
    void SpawnBombServerRpc( Vector3 cellCenter )
    {
        SpawnBombClientRpc(cellCenter);
    }
    [ServerRpc]
    void SpawnBombOptionOneServerRpc( Vector3 cellCenter )
    {
        SpawnBombOptionOneClientRpc(cellCenter);
    }
    [ServerRpc]
    void SpawnBombOptionTwoServerRpc( Vector3 cellCenter )
    {
        SpawnBombOptionTwoClientRpc(cellCenter);
    }


    private void Movement()
    {
        if( !GameManager.instance.isGameOver )
        {
            if( GameManager.instance.isStart )
            {
                if( dieing == false )
                {
                    float x = Input.GetAxis("Horizontal");
                    float y = Input.GetAxis("Vertical");
                    flip();
                    Vector2 movement = new Vector2(x , y);
                    rb.velocity = movement * _moveSpeed;
                    animator.SetFloat("velocity" , rb.velocity.magnitude);
                    if( Input.GetKeyDown(KeyCode.Space) )
                    {
                        PlaceBomb();
                    }
                }
            }
            else rb.velocity = Vector2.zero;
        }
    }


    private void flip()
    {
        if(Input.GetKeyDown(KeyCode.A) ) 
        {
            transform.localScale = new Vector3(1 , transform.localScale.y , transform.localScale.z);
        }
        if( Input.GetKeyDown(KeyCode.D) )
        {
            transform.localScale = new Vector3(-1 , transform.localScale.y , transform.localScale.z);
        }
    }

    public void ItemAbility(float speed , float duration  )
    {
        _moveSpeed = speed;
        _itemDuration = duration;
        StartCoroutine("SpeedDuration");
        SoundManager.instance.Play(Collect);
    }

   

    public void ItemProtection(bool protection, float duration )
    {
        Protection = protection;  
        _itemDuration = duration;
        StartCoroutine("ProtectionDuration");
        SoundManager.instance.Play(Collect);
    }

    public void ItemAddBomb(int addBomb ,int addmaxBomb)
    {
        currentBombs = addBomb;
        maxBombs = addmaxBomb;
        
        SoundManager.instance.Play(Collect);
        
    }

    public void ExplosionRangeOptionOne(bool explosion, float duration)
    {
        ExplosionRangeOne = explosion;
        _itemDuration = duration;
        StartCoroutine("ExplosionTwoDuration");
        SoundManager.instance.Play(Collect);
    }
    
    public void ExplosionRangeOptionTwo(bool explosion, float duration)
    {
        ExplosionRangeTwo = explosion;
        _itemDuration = duration;
        StartCoroutine("ExplosionOneDuration");
        SoundManager.instance.Play(Collect);
    }

    private IEnumerator SpeedDuration()
    {
        yield return new WaitForSeconds(_itemDuration);
        _moveSpeed = normalSpeed;
    }

    private IEnumerator ProtectionDuration()
    {
        yield return new WaitForSeconds(_itemDuration);
        Protection = false;
    }
    private IEnumerator ExplosionOneDuration()
    {
        yield return new WaitForSeconds(_itemDuration);
        ExplosionRangeOne = false;
    }
    private IEnumerator ExplosionTwoDuration()
    {
        yield return new WaitForSeconds(_itemDuration);
        ExplosionRangeTwo = false;
    }
    

    [ClientRpc]
    void TeamSelectClientRpc(int _teamNumber )
    {
        teamNumber = _teamNumber;
    }
    [ServerRpc]
    void TeamSelectServerRpc( int _teamNumber )
    {
        TeamSelectClientRpc(_teamNumber);
    }

    private bool afterplay;
    private void spawnpoint()
    {
        if( SceneManager.GetActiveScene().name == "InGame" )
        {
            if( isSpawn == false )
            {
                if( afterplay == false )
                {
                    transform.position = Spawnpoint.instance.spawnpoint[teamNumber].position;
                }
                if( afterplay == true) 
                {
                    int randomInt = UnityEngine.Random.Range(0 , Spawnpoint.instance.spawnpoint.Length);
                    transform.position = Spawnpoint.instance.spawnpoint[randomInt].position; 
                } 
                rb.velocity = Vector2.zero;
                afterplay = true;
            isSpawn = true;
            }
        }
    }

    public void selectTeam(int teamnuumber)
    {
        TeamSelectServerRpc(teamnuumber);
    }

    public void playerDie()
    {
        if (Protection == false)
        {
            SoundManager.instance.Play(Dead);
            dieing = true;
            StartCoroutine(waitRespawn());
            animator.SetBool("die" , true);
        }
        else
        {
            isSpawn = true;
        }
    }

    private IEnumerator waitRespawn()
    {
        yield return new WaitForSeconds(3);
        isSpawn = false;
        dieing = false;
        _moveSpeed = normalSpeed;
        ExplosionRangeOne = false;
        maxBombs = 3;
        currentBombs = maxBombs;
        animator.SetBool("die" , false);
    }
}

PlayerControll

Screenshot 2023-05-26 120503.png
Screenshot 2023-05-26 120430.png
Screenshot 2023-05-26 120353.png
Screenshot 2023-05-26 122818.png
Screenshot 2023-05-26 122857.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using System.Linq;
using Unity.Services.Lobbies.Models;

public class Bomb : NetworkBehaviour
{
    [SerializeField] private GameObject colorPrefab;
    public Vector3 cellPosition;
    private Grid grid;
    private GameObject _grid;
    private float cellSize = 1;
    private Collider2D col;
    public int _distance ;
    public bool _optionB;

    // Start is called before the first frame update
    void Start()
    {
        col = GetComponent<Collider2D>();
        if( grid == null )
        {
            _grid = GameObject.FindGameObjectWithTag("Grid");
            grid = _grid.GetComponent<Grid>();
        }

    }

    private void OnTriggerExit2D( Collider2D collision )
    {
        col.isTrigger = false;
    }

    public void DestroyObject()
    {
        // Get the parent object of the current object
        GameObject parentObject = transform.parent.gameObject;

        // Destroy the parent object
        Destroy(parentObject);
    }

    public void InstantiateObjectsOnCellAndDirections()
    {
        Vector3Int cellPosition = grid.WorldToCell(transform.position);
        Vector3 cellCenter = grid.GetCellCenterWorld(cellPosition);
        
        for( float i = -_distance  ; i <= _distance   ; i += cellSize   )
        {
            Vector3 horizontalPosition = cellCenter + new Vector3(i , 0 , 0);
            if( CheckForTagInCell(horizontalPosition) )
            {
                Instantiate(colorPrefab , horizontalPosition , Quaternion.identity);
            }
        }
        
        for( float j = -_distance  ; j <= _distance  ; j += cellSize  )
        {
            if( j == 0 ) continue; 
            Vector3 verticalPosition = cellCenter + new Vector3(0 , j , 0);
            if( CheckForTagInCell(verticalPosition) )
            {
                Instantiate(colorPrefab , verticalPosition , Quaternion.identity);
            }
        }
        
    }
   
    
    private void SpawnColor( Vector3 cellCenter )
    {
        Instantiate(colorPrefab , cellCenter , Quaternion.identity);
    }

    bool CheckForTagInCell( Vector3 cell )
    {

        // Calculate the bounds of the cell
        Bounds cellBounds = new Bounds(cell , new Vector3(cellSize , cellSize , 1));

        // Detect all colliders in the cell bounds
        Collider2D[] colliders = Physics2D.OverlapBoxAll(cellBounds.center , cellBounds.size , 0);


        foreach( Collider2D collider in colliders )
        {
            if (collider.transform.root.CompareTag("Player")  )
            {
                collider.GetComponentInParent<PlayerController>().playerDie();
            }
        }

        foreach( Collider2D collider in colliders )
        {
            if( collider.gameObject.CompareTag("OffStage") )
            {
                return false;
            }
        }

        // Check if any colliders have the "Color" tag
        foreach( Collider2D collider in colliders )
        {
            if( cellBounds.Contains(collider.transform.position) )
            {
                    // Check if the collider's game object has the specified tag
                    if( collider.gameObject.CompareTag("Color") )
                {
                    if( collider.gameObject.name == "Yellow(Clone)" && colorPrefab.name == "Yellow" )
                    {
                        return false;
                    }
                    if( collider.gameObject.name == "Purple(Clone)" && colorPrefab.name == "Purple" )
                    {
                        return false;
                    }
                    else Destroy(collider.gameObject);
                }
            }
        }
        

        return true;
    }
}

Bomb

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using Unity.Netcode.Components;

public class ItemSpawner : NetworkBehaviour
{
    [SerializeField] Grid grid;
    [SerializeField] private Transform[] spawnerPosition;
    [SerializeField] private GameObject[] ItemPrefab;
    [SerializeField] private float time = 30f;

    private void Awake()
    {
        
    }
    private void Start()
    {
        if( !IsServer ) return;
            StartCoroutine("ItemSpawnCountdown");
    }

    private IEnumerator ItemSpawnCountdown()
    {
        yield return new WaitForSeconds(time);
        SpawnItemServerRpc();
    }

    [ClientRpc]
    void SpawnItemClientRpc(int itemNo , Vector2 cellCenter )
    {
        Instantiate(ItemPrefab[itemNo] , cellCenter , Quaternion.identity);
    }

    [ServerRpc]
    void SpawnItemServerRpc()
    {
        if( !GameManager.instance.isGameOver )
        {
            int randomSpawnPosition = Random.Range(0 , spawnerPosition.Length);
            Vector3Int cellPosition = grid.WorldToCell(spawnerPosition[randomSpawnPosition].position);
            Vector3 cellCenter = grid.GetCellCenterWorld(cellPosition);
            int randomItem = Random.Range(0 , ItemPrefab.Length);
            SpawnItemClientRpc(randomItem , cellCenter);
            StartCoroutine("ItemSpawnCountdown");
        }
    }
}

ItemSpawner

Akaranan Weerayanphan

akaranan.weer@gmail.com

Tel : 0814323331

  • alt.text.label.Facebook

©2023 by Akaranan Weerayanphan. Proudly created with Wix.com

bottom of page