ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [게임엔진활용] 11강 피하기 게임 제작
    [DSU] 게임엔진활용 2026. 6. 8. 01:22
    728x90

    피하기 게임 제작

    - 계속 피해서 적이 모이면, 적이 한곳에 모이는 현상 발생 => 해결책 과제로 제출

    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
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    using UnityEngine;
    using UnityEngine.InputSystem;
     
    public class GameRog : RenderView
    {
        class Enemy
        {
            public Vector2 p, v;
            public float speed;
     
            public Vector2 tP;
            public float aiDt;
        }
     
        class Hero
        {
            public Vector2 p, v;
            public float speed, stamina;
     
            public Vector2 fv;
            public float fRate, fSpeed;
        }
        Hero pHero;
     
        Enemy[] enemy;
        int enemyNum;
        float createEnemyDt;
     
        public override void load()
        {
            pHero = new Hero();
            pHero.p = new Vector2(mc.devWidth / 2, mc.devHeight / 2);
            pHero.v = new Vector2(00);
            pHero.speed = 200;
            pHero.stamina = 100;
     
            enemy = new Enemy[100];
            enemyNum = 0;
            createEnemyDt = 0f;
        }
     
        public override void free()
        {
        }
     
        public override void draw(float dt)
        {
            // view
            setRGBA(0000.9f);
            fillRect(00, mc.devWidth, mc.devHeight);
     
            setRGBA(1111);
            fillRect(pHero.p.x - 25, pHero.p.y - 255050);
     
            setRGBA(1101);
            float w = 200 * pHero.stamina / 100;
            fillRect(5050, w, 5);
     
            setRGBA(1001);
            for (int i = 0; i < enemyNum; i++)
            {
                Enemy e = enemy[i];
                fillRect(e.p.x - 25, e.p.y - 255050);
                // ai apply
                if (e.p != e.tP)
                {
                    //e.p => e.tP; 이동
                    Vector2 vector = e.tP - e.p;
                    vector.Normalize();
     
                    //e.p += vector * dt;
                    Vector2 vt = vector * 200 * dt;
                    if (e.p.x < e.tP.x)
                    {
                        e.p.x += vt.x; if (e.p.x > e.tP.x) e.p.x = e.tP.x;
                    }
                    else if (e.p.x > e.tP.x)
                    {
                        e.p.x += vt.x; if (e.p.x < e.tP.x) e.p.x = e.tP.x;
                    }
                    if (e.p.y < e.tP.y)
                    {
                        e.p.y += vt.y; if (e.p.y > e.tP.y) e.p.y = e.tP.y;
                    }
                    else if (e.p.y > e.tP.y)
                    {
                        e.p.y += vt.y; if (e.p.y < e.tP.y) e.p.y = e.tP.y;
                    }
                }
                else
                {
                    e.aiDt += dt;
                    if (e.aiDt > 0.5f)
                    {
                        e.aiDt = 0f;
                        e.tP = pHero.p;
                    }
                }
     
                if ((pHero.p.x - e.p.x) * (pHero.p.x - e.p.x) +
                    (pHero.p.y - e.p.y) * (pHero.p.y - e.p.y) < 50 * 50)
                {
                    // hero dmg
                    enemyNum--;
                    enemy[i] = enemy[enemyNum];
                    i--;
                }
            }
            setRGBA(1111);
     
            if (enemyNum < 100)
            {
                createEnemyDt += dt;
                if (createEnemyDt > 5.0f)
                {
                    createEnemyDt -= 5.0f;
                    Enemy e = new Enemy();
                    e.p.x = getRandom() % mc.devWidth;
                    e.p.y = getRandom() % mc.devHeight;
                    e.tP = e.p;
     
                    enemy[enemyNum] = e;
                    enemyNum++;
                }
            }
     
            // ctrl
            Vector2 v = new Vector2(00);
            if (Keyboard.current.wKey.isPressed) v.y = -1;
            if (Keyboard.current.sKey.isPressed) v.y = +1;
            if (Keyboard.current.aKey.isPressed) v.x = -1;
            if (Keyboard.current.dKey.isPressed) v.x = +1;
            v.Normalize();
     
            if (Keyboard.current.spaceKey.isPressed)
            {
                if (pHero.stamina > 0)
                {
                    pHero.stamina -= 30 * Time.deltaTime;
                    v *= 2;
                }
            }
            else
            {
                if (pHero.stamina < 100)
                    pHero.stamina += 5 * Time.deltaTime;
            }
     
            pHero.p += v * pHero.speed * Time.deltaTime;
        }
     
        public override void mouse(int state, float x, float y)
        {
        }
    }
    cs

     

Designed by Tistory.