๐Ÿ“ฆ cart / godot3-bunnymark

๐Ÿ“„ BunnymarkV2.d ยท 120 lines
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
120module bunnymark.v2;

import std.random;
import std.stdio;
import std.conv;

import godot.c;
import godot, godot.core;
import godot.texture, godot.node2d, godot.label, godot.sprite;
import godot.resourceloader;

class BunnymarkV2 : GodotScript!Node2D 
{   
    alias owner this;
    @OnReady!((){return cast(Texture)ResourceLoader.load("res://images/godot_bunny.png");})
    Texture texture;

    @OnReady!((){return Label._new();})
    Label label;

    @OnReady!((){return Node2D._new();})
    Node2D bunnies;

    private {
        Vector2 screenSize;
        Vector2[] speeds;
        const float gravity = 500.0;
        const Color white = Color(1.,1.,1.,1.);
        const Texture nullTexture = Texture();
    }


    @Method 
    void _ready() 
    {
        this.addChild(bunnies);
        
        label.setPosition(Vector2(0,20));
        this.addChild(label);

        this.setProcess(true);
    }
    
    @Method 
    void _process(float delta)
    {
        screenSize = getViewportRect().size;
        auto count = bunnies.getChildCount();
        label.setText(text("Bunnies: ", count));

        Array children = bunnies.getChildren();
        for (int i; i < children.size(); i++)
        {
            auto bunny = children[i].as!Sprite;
            Vector2 position = bunny.getPosition();
            Vector2 speed = speeds[i];
            
            position.x += speed.x * delta;
            position.y += speed.y * delta;

            speed.y += gravity * delta;

            if (position.x > screenSize.x)
            {
                speed.x *= -1;
                position.x = screenSize.x;
            }
            if (position.x < 0)
            {
                speed.x *= -1;
                position.x = 0;
            }

            if (position.y > screenSize.y)
            {
                position.y = screenSize.y;
                if (uniform(0, 100.0) > 50.0)
                    speed.y = uniform(50, 1150);
                else
                    speed.y *= -0.85f;
            }

            if (position.y < 0)
            {
                speed.y = 0;
                position.y = 0;
            }

            bunny.setPosition(position);
            speeds[i] = speed;
        }
    }

    @Method @Rename("add_bunny") 
    void addBunny()
    {
        Sprite bunny = Sprite._new();
        bunny.setTexture(texture);
        bunnies.addChild(bunny);
        bunny.setPosition(Vector2(screenSize.x / 2.0, screenSize.y / 2.0));
        speeds ~= Vector2(uniform(50, 250), uniform(50,200));
    }

    @Method @Rename("remove_bunny") 
    void removeBunny()
    {
        immutable int count = bunnies.getChildCount();
        if (count == 0)
            return;
        bunnies.removeChild(bunnies.getChild(count - 1));
        speeds.length--;
    }

    @Method 
    void finish() {
        Array array = Array.empty_array();
        array.pushBack(Variant(bunnies.getChildCount()));
        owner.emitSignal("benchmark_finished", array);
    }
}