C#/수업과제

0322_ delegates 연습

minquu 2021. 3. 22. 18:12
반응형

1- 드라군 생성이 다 되었으면 사운드 나오기

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public delegate void OnCreateUnit();

    public class Dragoon
    {
        public OnCreateUnit createUnit;
        public Dragoon()
        {

        }
        public void CreateUnit()
        {
            Console.WriteLine("드라군이 생성됩니다.");
            this.createUnit();
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public class App
    {
        //생성자 
        public App()
        {
            Dragoon dragoon = new Dragoon();
            dragoon.createUnit = this.FinishedCreate;
            dragoon.CreateUnit();
        }
        public void FinishedCreate()
        {
            Console.WriteLine("드라군의 생성이 완료 되었습니다.");
            Console.WriteLine("드라군 voice : 리 시빙~");
        }
    }

}

 

------

2 레이스가 이동합니다.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public class Wraith
    {
        public delegate void MoveComplete();
        public MoveComplete moveComplete;
        public Wraith(){
        }
        public void Move() {
            Console.WriteLine("레이스가 이동합니다.");
            this.moveComplete();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public class App
    {
        //생성자 
        public App()
        {
            Wraith wraith = new Wraith();
            wraith.moveComplete = CompleteMove;
            wraith.Move();
        }
        public void CompleteMove()
        {
            Console.WriteLine("이동을 완료하였습니다.");

        }
    }

}

 

----

3. 매개변수가 들어간 ) 노래 재생

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public delegate void OnPlayMusic(string musicName);
    
    public class MusicBox
    {
        public OnPlayMusic onPlayMusic;
        public string musicName { get; private set; }
        public MusicBox(string musicName)
        {
            this.musicName = musicName;
        }
        public void PlayMusic()
        {
            Console.WriteLine("노래 [{0}] 이/가 재생됩니다.",this.musicName);
            this.onPlayMusic(this.musicName);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public class App
    {
        //생성자 
        public App()
        {
            MusicBox musicBox = new MusicBox("아이유 - 좋은 날");
            musicBox.onPlayMusic = this.CompletePlay;
            musicBox.PlayMusic();
        }
        public void CompletePlay(string musicName) {
            Console.WriteLine("[{0}]이/가 재생 완료되었습니다.",musicName);
        }
    }

}

 

-----

 

4. (매개변수)피자(피자이름)를 만들기 - > 완료

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public delegate void OnMakePizza(string name);

    public class Pizza
    {
        public OnMakePizza OnMakePizza;
        public string name { get; private set; }
        public Pizza(string name) {
            this.name = name;
        }
        public void MakePizza()
        {
            Console.WriteLine("[{0}] 피자를 만들고 있습니다.",this.name);
            this.OnMakePizza(this.name);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public class App
    {
        //생성자 
        public App()
        {
            Pizza pizza = new Pizza("고구마");
            pizza.OnMakePizza = MakeComplete;
            pizza.MakePizza();
        }
        public void MakeComplete(string name)
        {
            Console.WriteLine("[{0}] 피자를 다 완성하였습니다.",name);
        }
    }

}

 

 

----

5. SCV 생성 후  -> 배럭을 짓는다 (특정 시간 후 완성)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public delegate void OnCreateUnit();
    public class SCV
    {
        public OnCreateUnit onCreateUnit;
        public SCV()
        {

        }
        public void Make() {
            Console.WriteLine("SCV 를 생성합니다.");
            this.onCreateUnit();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace study10
{
    public delegate void DelCompleteBuild();
    public class Barrack
    {
        public DelCompleteBuild delCompleteBuild;
        public Barrack()
        {
            
        }
        public void Built() {
            Console.WriteLine("배럭이 지어집니다.");
            for (int i = 0; i < 11; i++) {
                Thread.Sleep(1000);
                Console.WriteLine("[{0}]% 완성되었습니다.", i * 10);
            }
            this.delCompleteBuild();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study10
{
    public class App
    {
        //생성자 
        public App() {
            SCV scv = new SCV();
            scv.onCreateUnit = CompleteCreate;
            scv.Make();

            Barrack barrack = new Barrack();
            barrack.delCompleteBuild = CompleteBuild;
            barrack.Built();
        }
        public void CompleteCreate() {
            Console.WriteLine("SCV 를 만들었습니다.");
        }
        public void CompleteBuild()
        {
            Console.WriteLine("건물을 완성하였습니다.");
        }
    }

}

 

---

6. 저글링 생성 후 -> 이동 ( Action를 사용해서)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study7_hw
{
    public class Zergling
    {
        public float x;
        public float y;
        public Action onCreate;
        public Action<float, float> onMoveComplete;
        public Zergling(float x, float y){
            this.x = x;
            this.y = y;
        }
        public void CreatUnit() {
            Console.WriteLine("저글링을 생성합니다.");
            this.onCreate();
        }
        public void Move(float x, float y) {
            this.x = x;
            this.y = y;
            this.onMoveComplete(this.x, this.y);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study7_hw
{
    public class App
    {
        public App(){
            Console.WriteLine("APP 생성자");
            Zergling zergling = new Zergling(0,0);
            zergling.onCreate = CreateUnitComplete;
            zergling.CreatUnit();
            zergling.onMoveComplete = MoveComplete;
            zergling.Move(1,3);
        }
        private void CreateUnitComplete() {
            Console.WriteLine("저글링 생성이 완료 되었습니다.");
        }
        private void MoveComplete(float x, float y)
        {
            Console.WriteLine("저글링 이동 완료 : [{0}] [{1}]", x, y);
        }
        
    }
}

 

----

7. 맥크리와 트레이서 (맥크리 총쏘고난 뒤 이펙트, 트레시어 죽을 시 액션)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study7_hw
{
    public class McCree
    {
        public Action onAttack;
        public int damage = 15;
        public McCree()
        {

        }
        public void Attack(Tracer tracer) {
            Console.WriteLine("맥크리가 트레이서를 공격했습니다.");
            tracer.Hit(this.damage);
            this.onAttack();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study7_hw
{
    public class Tracer
    {
        private int hp;
        private int maxhp;
        public Action onDie;
        public Tracer()
        {
            this.maxhp = 50;
            this.hp = this.maxhp;
        }
        public void Hit(int damage)
        {
            if (this.hp > 0) {
                Console.WriteLine("트레이서가 피해 [{0}] 를 입었습니다.", damage);
                this.hp -= damage;
                Console.WriteLine("현재 체력 : {0} / {1}", this.hp, this.maxhp);
            }
            else if (this.hp <= 0) 
            {
                Console.WriteLine("트레이서가 죽었습니다.");
                this.onDie();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study7_hw
{
    public class App
    {
        public App(){
            McCree mcCree = new McCree();
            Tracer tracer = new Tracer();
            mcCree.onAttack = AttackComplete;
            tracer.onDie = DieComplete;
            mcCree.Attack(tracer);
            mcCree.Attack(tracer);
            mcCree.Attack(tracer);
            mcCree.Attack(tracer);
            mcCree.Attack(tracer);

        }
        public void AttackComplete() {
            Console.WriteLine("총 이펙트 : 탕!");
        }
        public void DieComplete() {
            Console.WriteLine("죽음 이펙트 : 으억!");
        }
    }
}

 

반응형

'C# > 수업과제' 카테고리의 다른 글

0324_ JSON 파일 리스트화  (0) 2021.03.24
0323_ 람다식 연습  (0) 2021.03.24
0321 _ 현재 본인 판단 표  (0) 2021.03.21
0318_ArrayList, List<T>, Dictionary 예제 연습  (0) 2021.03.19
0316_ 클래스 생성연습, 배열 연습,  (0) 2021.03.17