File: compat.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 (131 lines) | stat: -rw-r--r-- 3,258 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
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
// -----------------------------------------------------------------------------
// File: compat.ss
// Description: glue-code for retro-compatibility with built-in items (legacy)
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------
using SurgeEngine.Level;
using SurgeEngine.Vector2;
using SurgeEngine.Transform;
using SurgeEngine.Events.EntityEvent;
using SurgeEngine.Events.FunctionEvent;

object ".compat_loopgreen" is "private", "entity"
{
    obj = spawn("Layer Green");

    state "main"
    {
    }

    fun constructor()
    {
        transform = obj.child("Transform") || obj.spawn("Transform");
        transform.translateBy(16, 16);
    }
}

object ".compat_loopyellow" is "private", "entity"
{
    obj = spawn("Layer Yellow");

    state "main"
    {
    }

    fun constructor()
    {
        transform = obj.child("Transform") || obj.spawn("Transform");
        transform.translateBy(16, 16);
    }
}

object ".compat_perspikes" is "private", "entity"
{
    spikes = spawn("Spikes");

    state "main"
    {
    }

    fun constructor()
    {
        spikes.periodic = true;
    }
}

object ".compat_perceilspikes" is "private", "entity"
{
    spikes = spawn("Spikes Down");

    state "main"
    {
    }

    fun constructor()
    {
        spikes.periodic = true;
    }
}

object ".compat_switch" is "private", "entity"
{
    obj = spawn("Switch");
    transform = Transform();

    state "main"
    {
        // get the closest entities
        position = transform.position;
        door = findClosestEntity("Door");
        teleporter = findClosestEntity("Teleporter");

        // configure the switch
        if(door != null || teleporter != null) {
            doorDistance = (door != null) ? position.distanceTo(entityPosition(door)) : Math.infinity;
            teleporterDistance = (teleporter != null) ? position.distanceTo(entityPosition(teleporter)) : Math.infinity;
            if(doorDistance < teleporterDistance) {
                obj.sticky = false;
                obj.onActivate = EntityEvent(door).willCall("open");
                obj.onDeactivate = EntityEvent(door).willCall("close");
            }
            else {
                obj.sticky = true;
                obj.onActivate = EntityEvent(teleporter).willCall("teleport");
            }
        }

        // done
        state = "done";
    }

    state "done"
    {
    }

    // you must ensure that entityName references objects tagged "entity"
    fun findClosestEntity(entityName)
    {
        closestEntity = null;
        closestDistance = Math.infinity;
        position = transform.position;

        entities = Level.findEntities(entityName);
        foreach(entity in entities) {
            distance = position.distanceTo(entityPosition(entity));
            if(distance < closestDistance) {
                closestEntity = entity;
                closestDistance = distance;
            }
        }

        return closestEntity;
    }

    // get the world position of an entity
    fun entityPosition(entity)
    {
        entityTransform = entity.child("Transform") || entity.spawn("Transform");
        return entityTransform.position;
    }
}