C#/문제해결

1005 공격구현

버스창문 2022. 10. 5. 11:00
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Tilemaps;
using Newtonsoft.Json;

public class TileScan : MonoBehaviour
{
    public TileMapData tileMapData;
    //public Grid tileMapGOGrid;
    public Tilemap tileMapGO;
    //public TilemapRenderer tilemapRenderer;
    public Vector2Int bottomLeft, topRight;
    private int a;
    MonsterAttack mon = null;
    // Start is called before the first frame update
    void Start()
    {
        this.tileMapData = new TileMapData();
        //this.tileMapData.bottomLeft = this.bottomLeft;
        this.tileMapData.bottomLeft = new Vector2Int(this.tileMapGO.cellBounds.xMin,this.tileMapGO.cellBounds.yMin);
        //this.tileMapData.topRight = this.topRight;
        this.tileMapData.topRight = new Vector2Int(this.tileMapGO.cellBounds.xMax, this.tileMapGO.cellBounds.yMax);
        this.tileMapData.sizeX = this.tileMapData.topRight.x - this.tileMapData.bottomLeft.x + 1;
        Debug.Log(this.tileMapData.sizeX);
        this.tileMapData.sizeY = this.tileMapData.topRight.y - this.tileMapData.bottomLeft.y + 1;
        this.tileMapData.TileArray = new Tile[this.tileMapData.sizeX, this.tileMapData.sizeY];

        for (int i = 0; i < this.tileMapData.sizeY; i++)
        {
            for (int j = 0; j < this.tileMapData.sizeX; j++)
            {
                bool isWall = false;
                bool isPlayer = false;
                bool isMonster = false;
                foreach (Collider2D col in Physics2D.OverlapCircleAll(new Vector2(j + bottomLeft.x - 0.5f, i + bottomLeft.y - 0.5f), 0.3f))
                {
                    
                    if (col.gameObject.layer == LayerMask.NameToLayer("Water")) 
                    {
                        Debug.LogFormat("벽 {0} , {1}", j, i);
                        isWall = true; 
                    }
                    else if (col.gameObject.layer == LayerMask.NameToLayer("Player"))
                    {
                        Debug.LogFormat("사람 {0} , {1}",j,i);
                        isPlayer = true;
                    }
                    else if (col.gameObject.layer == LayerMask.NameToLayer("Monster"))
                    {
                        Debug.LogFormat("몬스터 {0} , {1}", j, i);
                        isMonster = true;
                        this.mon = col.gameObject.GetComponent<MonsterAttack>();
                    }
                }
                a++;
                this.tileMapData.TileArray[j, i] = new Tile(isWall, isPlayer, j + bottomLeft.x, i + bottomLeft.y);
                if (isMonster)
                {
                    this.mon.monPos = new Vector2Int(j, i);
                    Debug.Log(this.mon.monPos);
                    this.mon.attackAction = (attackRange , monPos) =>
                    {
                        Debug.Log("공격");
                        foreach (var a in attackRange)
                        {
                            if(this.tileMapData.TileArray[monPos.x + a.x ,monPos.y+ a.y].isPlayer)
                            {
                                Debug.Log("공격성공");
                            }
                        }
                    };
                }
            }
        }
        Debug.Log(a);
        //주소는 바꿔야함 임시임
        //File.WriteAllText(@"D:\workspaces\svn\Client\Test\ProtoType\Tables\Test.json", JsonConvert.SerializeObject(this.tileMapData));
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class MonsterAttack : MonoBehaviour
{
    public List<Vector2Int> range;
    public Vector2Int monPos;
    public float judgeDelay;
    public float castDelay;
    public float coolTime;
    public bool isProjectile;
    public float speed;
    public int direction;

    public UnityAction<List<Vector2Int> , Vector2Int> attackAction;

    // Start is called before the first frame update
    void Start()
    {
        this.range = new List<Vector2Int>();
        this.range.Add(new Vector2Int(-1,1));
        this.range.Add(new Vector2Int(0, 1));
        this.range.Add(new Vector2Int(1, 1));
        this.range.Add(new Vector2Int(-1, 2));
        this.range.Add(new Vector2Int(0, 2));
        this.range.Add(new Vector2Int(1, 2));
        this.judgeDelay = 1;
        this.castDelay = 1;
        this.coolTime = 1;
        this.isProjectile = true;
        this.speed = 1;
        this.direction = 1;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            this.attackAction(this.range, this.monPos);
        }
    }
}

단순 범위 공격

 

 

 

 

 

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Tilemaps;
using Newtonsoft.Json;

public class TileScan : MonoBehaviour
{
    public TileMapData tileMapData;
    //public Grid tileMapGOGrid;
    public Tilemap tileMapGO;
    //public TilemapRenderer tilemapRenderer;
    public Vector2Int bottomLeft, topRight;
    private int a;
    MonsterAttack mon = null;
    // Start is called before the first frame update
    void Start()
    {
        this.tileMapData = new TileMapData();
        //this.tileMapData.bottomLeft = this.bottomLeft;
        this.tileMapData.bottomLeft = new Vector2Int(this.tileMapGO.cellBounds.xMin,this.tileMapGO.cellBounds.yMin);
        //this.tileMapData.topRight = this.topRight;
        this.tileMapData.topRight = new Vector2Int(this.tileMapGO.cellBounds.xMax, this.tileMapGO.cellBounds.yMax);
        this.tileMapData.sizeX = this.tileMapData.topRight.x - this.tileMapData.bottomLeft.x + 1;
        Debug.Log(this.tileMapData.sizeX);
        this.tileMapData.sizeY = this.tileMapData.topRight.y - this.tileMapData.bottomLeft.y + 1;
        this.tileMapData.TileArray = new Tile[this.tileMapData.sizeX, this.tileMapData.sizeY];

        for (int i = 0; i < this.tileMapData.sizeY; i++)
        {
            for (int j = 0; j < this.tileMapData.sizeX; j++)
            {
                bool isWall = false;
                bool isPlayer = false;
                bool isMonster = false;
                foreach (Collider2D col in Physics2D.OverlapCircleAll(new Vector2(j + bottomLeft.x - 0.5f, i + bottomLeft.y - 0.5f), 0.3f))
                {
                    
                    if (col.gameObject.layer == LayerMask.NameToLayer("Water")) 
                    {
                        Debug.LogFormat("벽 {0} , {1}", j, i);
                        isWall = true; 
                    }
                    else if (col.gameObject.layer == LayerMask.NameToLayer("Player"))
                    {
                        Debug.LogFormat("사람 {0} , {1}",j,i);
                        isPlayer = true;
                    }
                    else if (col.gameObject.layer == LayerMask.NameToLayer("Monster"))
                    {
                        Debug.LogFormat("몬스터 {0} , {1}", j, i);
                        isMonster = true;
                        this.mon = col.gameObject.GetComponent<MonsterAttack>();
                    }
                }
                a++;
                this.tileMapData.TileArray[j, i] = new Tile(isWall, isPlayer, j + bottomLeft.x, i + bottomLeft.y);
                if (isMonster)
                {
                    this.mon.monPos = new Vector2Int(j, i);
                    Debug.Log(this.mon.monPos);
                    this.mon.attackAction = (attackRange , monPos) =>
                    {
                        Debug.Log("공격");
                        foreach (var a in attackRange)
                        {
                            if(this.tileMapData.TileArray[monPos.x + a.x ,monPos.y+ a.y].isPlayer)
                            {
                                Debug.Log("공격성공");
                            }
                        }
                    };
                    this.mon.AlertAttackAction = (attackRange, monPos, judgeDelay) =>
                    {
                        StartCoroutine(this.TimeCount(attackRange, monPos, judgeDelay));
                    };
                }
            }
        }
        Debug.Log(a);
        //주소는 바꿔야함 임시임
        //File.WriteAllText(@"D:\workspaces\svn\Client\Test\ProtoType\Tables\Test.json", JsonConvert.SerializeObject(this.tileMapData));
    }
    private IEnumerator TimeCount(List<Vector2Int> attackRange, Vector2Int monPos, float judgeDelay)
    {
        float tic = 0;
        while (judgeDelay > tic)
        {
            tic += Time.deltaTime;
            Debug.Log(tic);
            yield return null;
        }
        Debug.Log("시간 끝 공격 시작");
        foreach (var a in attackRange)
        {
            if (this.tileMapData.TileArray[monPos.x + a.x, monPos.y + a.y].isPlayer)
            {
                Debug.Log("공격성공");
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class MonsterAttack : MonoBehaviour
{
    public List<Vector2Int> range;
    public Vector2Int monPos;
    public float judgeDelay;
    public float castDelay;
    public float coolTime;
    public bool isProjectile;
    public float speed;
    public int direction;

    public UnityAction<List<Vector2Int> , Vector2Int> attackAction;
    public UnityAction<List<Vector2Int>, Vector2Int , float> AlertAttackAction;

    // Start is called before the first frame update
    void Start()
    {
        this.range = new List<Vector2Int>();
        this.range.Add(new Vector2Int(-1,1));
        this.range.Add(new Vector2Int(0, 1));
        this.range.Add(new Vector2Int(1, 1));
        this.range.Add(new Vector2Int(-1, 2));
        this.range.Add(new Vector2Int(0, 2));
        this.range.Add(new Vector2Int(1, 2));
        this.judgeDelay = 1;
        this.castDelay = 1;
        this.coolTime = 1;
        this.isProjectile = true;
        this.speed = 1;
        this.direction = 1;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            this.attackAction(this.range, this.monPos);
        }
        if (Input.GetMouseButtonDown(1))
        {
            this.AlertAttackAction(this.range, this.monPos, 3);
        }
    }
}

 

 

예고 공격

 

코드 상태는 개 박살이 나있는 관계로 다시 생각해봐야하지만 어쨋든 저렇게 코루틴 이용해서 만들게 될것같음