ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [게임엔진활용] 9강 플랫포머 게임 제작
    [DSU] 게임엔진활용 2026. 6. 8. 01:08
    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
    using UnityEngine;
    using UnityEngine.InputSystem;
     
    public class GameCollision : RenderView
    {
        struct ColObj
        {
            public Color c; // index
            public Rect rt;// image, texture
        }
        ColObj[] co;
     
        float gravity = 720 * 4;// d / s
        float jump = 0;
        int jumpNum = 0;
     
        public override void load()
        {
            Rect[] rect = new Rect[6] {
                new Rect(20065012040),
                new Rect(50055012040),
                new Rect(80045012040),
                new Rect(100035012040),
                new Rect(70015012040),
                new Rect(40020012040),
            };
     
            co = new ColObj[8];
            // 0 ~ 5 발판
            for (int i = 0; i < 6; i++)
            {
                co[i] = new ColObj();
                co[i].c = Color.red;
                co[i].rt = rect[i];
            }
            // hero
            ColObj c = new ColObj();
            c.c = Color.black;
            c.rt = new Rect(0, mc.devHeight - 60 - 5006060);
            co[6= c;
            // item
            c = new ColObj();
            c.c = Color.yellow;
            c.rt = new Rect(400200 - 606060);
            co[7= c;
        }
     
        public override void free()
        {
        }
     
        public override void draw(float dt)
        {
            setRGBA(0000.5f);
            fillRect(00, mc.devWidth, mc.devHeight);
     
            for (int i = 0; i < 8; i++)
            {
                ColObj c = co[i];
                Rect rt = c.rt;
                setRGBA(c.c.r, c.c.g, c.c.b, c.c.a);
                fillRect(rt.x, rt.y, rt.width, rt.height);
            }
            setRGBA(1111);
     
            ref ColObj hero = ref co[6];
            if (Keyboard.current.spaceKey.wasPressedThisFrame)
            {
                if (jumpNum < 2)
                {
                    jumpNum++;
                    Debug.Log("jumpNum = " + jumpNum);
                    jump = -700;
                }
            }
            if (Keyboard.current.aKey.isPressed)
            {
                hero.rt.x -= 300 * dt;
            }
            else if (Keyboard.current.dKey.isPressed)
            {
                hero.rt.x += 300 * dt;
            }
     
            jump += gravity * dt;
            if (jump > gravity)
                jump = gravity;
            hero.rt.y += jump * dt;
     
            float h = mc.devHeight - hero.rt.height;
     
            for (int i = 0; i < 6; i++)
            {
                ref ColObj obj = ref co[i];
                if (containRect(hero.rt, obj.rt))
                {
                    if (hero.rt.y > obj.rt.y - hero.rt.height - 1)
                        h = obj.rt.y - hero.rt.height - 1;
                    break;
                }
            }
            ref ColObj item = ref co[7];
            if (item.c != Color.clear &&
                containRect(hero.rt, item.rt))
            {
                item.c = Color.clear;
                Debug.Log("GameOver");
            }
     
            if (hero.rt.y > h)
            {
                hero.rt.y = h;
                jumpNum = 0;
            }
        }
     
        public override void mouse(int state, float x, float y)
        {
        }
     
    }
    cs

     

     

Designed by Tistory.