File: explosion_combo.ss

package info (click to toggle)
opensurge 0.6.1.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 47,404 kB
  • sloc: ansic: 62,066; sh: 739; makefile: 193; java: 110; xml: 66; javascript: 53
file content (79 lines) | stat: -rw-r--r-- 2,265 bytes parent folder | download
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
// -----------------------------------------------------------------------------
// File: explosion_combo.ss
// Description: explosion combo script
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------
using SurgeEngine.Transform;
using SurgeEngine.Vector2;
using SurgeEngine.Level;

//
// An Explosion Combo is a set of explosions that
// occur within a certain area, during some time
//
// Functions:
//
// - setSize(width, height):  Sets the size of the area of the explosion combo.
//                            The area is centered on the position of this object.
//                            Returns this.
//
// - setDuration(seconds):    Sets the duration of the explosion combo.
//                            Returns this.
//
object "Explosion Combo" is "private", "entity"
{
    transform = Transform();
    width = 64; // width of the explosion area, in pixels
    height = 64; // height of the explosion area, in pixels
    explosionCount = 16; // explode no more than explosionCount times
    explosionTime = 0.125; // seconds
    duration = 1.0; // seconds
    timer = 0.0;
    
    state "main"
    {
        // explosion timers
        if(!timeout(duration)) {
            timer += Time.delta;
            if(timer >= explosionTime) {
                explode();
                timer -= explosionTime;
            }
        }
        else
            destroy();
    }

    fun explode()
    {
        // compute the explosion offset
        lenx = Math.floor(width / 8);
        leny = Math.floor(height / 4);
        gridx = Math.floor(Math.random() * (1 + lenx)) - lenx / 2;
        gridy = Math.floor(Math.random() * (1 + leny)) - leny / 2;

        // create explosion
        Level.spawnEntity("Explosion",
            transform.position.translatedBy(
                8 * gridx,
                4 * gridy
            )
        );
    }

    // Define the size of the area
    fun setSize(w, h)
    {
        width = Math.max(w, 0);
        height = Math.max(h, 0);
        return this;
    }

    // Define the duration of the explosion combo
    fun setDuration(seconds)
    {
        duration = Math.max(seconds, 0);
        return this;
    }
}