| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                            Tags
                            
                        
                          
                          - Oculus
- 드래곤 플라이트
- meta xr
- 유니티
- VR
- Amazon S3
- CGV
- 오브젝트 풀링
- OVR
- 가상현실
- 개발일지
- 아마존
- ChatGPT
- 유니티 Json 데이터 연동
- 팀 프로젝트
- 모작
- 오큘러스
- 연습
- AWS
- Photon Fusion
- 팀프로젝트
- 드래곤 플라이트 모작
- 포트폴리오
- 유니티 GUI
- 개발
- 유니티 UI
- 길건너 친구들
- 멀티플레이
- meta
- Unity
                            Archives
                            
                        
                          
                          - Today
- Total
EasyCastleUNITY
2023/07/24- 클래스 및 메서드 활용 본문
마린이 저글링 공격, 메딕이 마린 회복
저글링
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
    internal class Zergling
    {
        //멤버 변수
        int maxHp = 35;
        int hp;
        int damage = 5;
        float moveSpeed = 2.612f;
        int x, y; //위치
        //기본생성자
        public Zergling()
        {
            hp = maxHp;
            Console.WriteLine("저글링이 생성되었습니다.");
            Console.WriteLine("저글링 생명력: {0}", this.hp);
            Console.WriteLine("저글링 공격력: {0}", this.damage);
            Console.WriteLine("저글링 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("------------------------");
        }
        public Zergling(int hp, int damage, float moveSpeed)
        {
            this.hp = hp;
            this.damage = damage;
            this.moveSpeed = moveSpeed;
            Console.WriteLine("저글링이 생성되었습니다.");
            Console.WriteLine("저글링 생명력: {0}", this.hp);
            Console.WriteLine("저글링 공격력: {0}", this.damage);
            Console.WriteLine("저글링 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("저글링 좌표: ({0},{1})", this.x, this.y);
            Console.WriteLine("------------------------");
        }
        //멤버 메서드
        //생명력을 반환하는 메서드
        public int GetHp() { return this.hp; }
        //공격력을 반환하는 메서드
        public int GetDamage()
        {
            return this.damage;
        }
        public float GetMoveSpeed()
        {
            return this.moveSpeed;
        }
        public void MoveStop()
        {
            Console.WriteLine("저글링 정지했습니다\n");
        }
        public void Move()
        {
            Console.WriteLine("저글링 이동했습니다\n");
        }
        public void Attack()
        {
            Console.WriteLine("저글링 공격했습니다\n");
        }
        public void Die()
        {
            Console.WriteLine("저글링 사망했습니다\n");
        }
        //피해를 받는 메서드 
        public void HitDamage(Marine marine, int damage)
        {
            this.hp -= damage;
            Console.WriteLine("공격자:{0}", marine);
            Console.WriteLine("피해({0})를 받았습니다, 저글링 생명력:{1}", damage,this.hp);
        }
        //좌표 설정 메서드
        public void SetPosition(int x, int y)
        {
            this.x = x;
            this.y = y;
            Console.WriteLine("좌표설정완료: ({0},{1})",this.x,this.y);
        }
        //상태출력
        public void PrintProperties()
        {
            Console.WriteLine("저글링 생명력: {0}", this.hp);
            Console.WriteLine("저글링 공격력: {0}", this.damage);
            Console.WriteLine("저글링 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("저글링 좌표: ({0},{1})", this.x,this.y);
            Console.WriteLine("------------------------");
        }
    }
}마린
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
    internal class Marine
    {
        //멤버 변수:객체의 생명주기동안 유지된다.
        int maxHp = 40; //최대 생명력
        int hp;         //현재 생명력
        int damage = 6; //공격력
        float moveSpeed=1.875f; //이동속도
        int x, y; //위치
        //기본 생성자
        public Marine()
        {
            hp = maxHp;
            this.x = 0; this.y = 0; 
            Console.WriteLine("마린이 생성되었습니다.");
            Console.WriteLine("마린 생명력: {0}",hp);
            Console.WriteLine("마린 공격력: {0}", damage);
            Console.WriteLine("마린 이동속도: {0}", moveSpeed);
            Console.WriteLine("------------------------");
        }
        //매개변수 생성자
        public Marine(int maxHp, int hp,int damage,float moveSpeed,int x, int y)
        {
            this.maxHp = maxHp;
            this.hp = hp;
            this.damage = damage;
            this.moveSpeed = moveSpeed;
            this.x = x;
            this.y = y;
            Console.WriteLine("마린이 생성되었습니다.");
            Console.WriteLine("마린의 위치 ({0},{1})", this.x, this.y);
            Console.WriteLine("마린 생명력: {0}", this.hp);
            Console.WriteLine("마린 공격력: {0}", this.damage);
            Console.WriteLine("마린 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("------------------------");
        }
        //멤버 메서드
        
        //생명력을 반환하는 메서드
        public int GetHp() { return this.hp; }
        //공격력을 반환하는 메서드
        public int GetDamage()
        {
            return this.damage;
        }
        public float GetMoveSpeed()
        {
            return this.moveSpeed;
        }
        public void MoveStop()
        {
            Console.WriteLine("마린 정지했습니다\n");
        }
        public void Move(int x, int y) //이동 목표 좌표
        {
            Console.WriteLine("({0},{1}) -> ({2},{3})로 이동했습니다",this.x,this.y,x,y);
            this.x = x;
            this.y = y;
        }
        //저글링 공격 메서드
        public void Attack(Zergling target)
        {
            Console.WriteLine("{0}을 마린이 공격했습니다\n",target);
            target.HitDamage(this ,this.damage); //공격력 만큼 피해를 받는 메서드
            //this는 클래스의 현재 인스턴스를 가리키므로
            //매개변수로 사용할수 있다. 
        }
        public void HitDamage(int damage)
        {
            this.hp -= damage;
            Console.WriteLine("피해({0})를 받았습니다, 마린 생명력:{1}/{2}", damage, this.hp,this.maxHp);
        }
        public void IncreaseHp(float heal)
        {
            //float floatHp = Convert.ToSingle(this.hp);
            //floatHp += heal;
            //this.hp = Convert.ToInt32(floatHp);
            this.hp += (int)heal;
            if (this.hp >= this.maxHp) {this.hp = maxHp; } 
            Console.WriteLine("회복({0})을 받았습니다, 마린 생명력:{1}/{2}",heal,this.hp,this.maxHp);
        }
        public void Die()
        {
            Console.WriteLine("마린 사망했습니다\n");
        }
        public void PrintProperties()
        {
            Console.WriteLine("마린의 위치 ({0},{1})", this.x, this.y);
            Console.WriteLine("마린 생명력: {0}/{1}", this.hp,this.maxHp);
            Console.WriteLine("마린 공격력: {0}", this.damage);
            Console.WriteLine("마린 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("------------------------");
        }
    }
}메딕
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
    internal class Medic
    {
        //멤버 변수
        int maxHp = 60;
        int hp;
        float heal = 5.86f;
        float moveSpeed = 1.875f;
        int x, y; //위치
        //기본 생성자
        public Medic()
        {
            hp = maxHp;
            Console.WriteLine("의무관이 생성되었습니다.");
            Console.WriteLine("의무관 생명력: {0}", this.hp);
            Console.WriteLine("의무관 초당치료량: {0}", this.heal);
            Console.WriteLine("의무관 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("------------------------");
        }
        //매개변수 생성자
        public Medic(int hp, float heal, float moveSpeed)
        {
            this.hp = hp;
            this.heal = heal;
            this.moveSpeed = moveSpeed;
            Console.WriteLine("의무관이 생성되었습니다.");
            Console.WriteLine("의무관 생명력: {0}", this.hp);
            Console.WriteLine("의무관 초당치료량: {0}", this.heal);
            Console.WriteLine("의무관 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("------------------------");
        }
        //멤버 메서드
        //생명력을 반환하는 메서드
        public int GetHp() { return this.hp; }
        //치료량을 반환하는 메서드
        public float GetHeal()
        {
            return this.heal;
        }
        //이동속도를 반환하는 메서드
        public float GetMoveSpeed()
        {
            return this.moveSpeed;
        }
        public void MoveStop()
        {
            Console.WriteLine("의무관이 정지했습니다\n");
        }
        public void Move()
        {
            Console.WriteLine("의무관이 이동했습니다\n");
        }
        public void Heal(Marine target)
        {
            Console.WriteLine("{0}에게 의무관이 회복을 시전했습니다\n", target);
            target.IncreaseHp(this.heal);
        }
        public void Die()
        {
            Console.WriteLine("의무관이 사망했습니다\n");
        }
        //상태출력
        public void PrintProperties()
        {
            Console.WriteLine("의무관 생명력: {0}", this.hp);
            Console.WriteLine("의무관 초당치료량: {0}", this.heal);
            Console.WriteLine("의무관 이동속도: {0}", this.moveSpeed);
            Console.WriteLine("------------------------");
        }
    }
}앱
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
    internal class App
    {
        //생성자
        public App()
        {
            //Marine marine = new Marine(40,6,1.875f,5,0);
            //Zergling zergling = new Zergling(35, 5, 2.61f);
            //zergling.SetPosition(4, 0); //좌표를 설정하는 메서드
            ////마린 -> 저글링 공격
            //marine.Attack(zergling);
            Marine marine = new Marine(40,40,6,1.875f,5,0);
            marine.HitDamage(3);
            Medic medic = new Medic(60, 5.8f, 1.8f);
            medic.Heal(marine);
        }
    }
}실행
using System;
namespace LearnDotnet
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //App 클래스의 인스턴스 생성하고 생성자를 호출함
            new App();
            
        }
    }
}
'C#프로그래밍' 카테고리의 다른 글
| 2023/07/24 복습 (0) | 2023.07.25 | 
|---|---|
| 배열 연습 Inventory(정렬 안함, 정렬함 + 아이템 개수 스택) (0) | 2023.07.24 | 
| 플레이어,무기,몬스터 (OOP 연습 및, 반환값, 매개변수 연습) (0) | 2023.07.21 | 
| 스타크래프트 테란 커맨드센터/SCV/SiezeTank(OOP 및 클래스 연습) (0) | 2023.07.21 | 
| switch if 문 (0) | 2023.07.21 | 
 
                   
                  