File: switch.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 (89 lines) | stat: -rw-r--r-- 2,170 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
// -----------------------------------------------------------------------------
// File: switch.ss
// Description: script for an on/off switch
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------
using SurgeEngine.Actor;
using SurgeEngine.Brick;
using SurgeEngine.Audio.Sound;
using SurgeEngine.Events.Event;
using SurgeEngine.Events.EntityEvent;
using SurgeEngine.Collisions.CollisionBox;

object "Switch" is "entity", "gimmick"
{
    public sticky = true; // a sticky switch is activated only once
    public onActivate = Event();
    public onDeactivate = Event();
    public color = "yellow"; // must be: "yellow", "green", "blue" or "red"

    sfx = Sound("samples/switch.wav");
    actor = Actor("Switch");
    brick = Brick("Switch Mask");
    collider = CollisionBox(22, actor.height).setAnchor(0.5, 1);
    pressed = false;

    state "main"
    {
        // pick the right animation
        selectedColor = String(color).toLowerCase();
        if(selectedColor == "yellow")
            actor.anim = 0;
        else if(selectedColor == "blue")
            actor.anim = 2;
        else if(selectedColor == "red")
            actor.anim = 4;
        else if(selectedColor == "green")
            actor.anim = 6;
        else
            actor.anim = 0;

        // done
        state = "idle";
    }

    state "idle"
    {
    }

    state "active"
    {
        if(!sticky && !pressed)
            deactivate();
        pressed = false;
    }

    fun onOverlap(otherCollider)
    {
        entity = otherCollider.entity;
        if(entity.hasTag("player") || entity.hasTag("weight")) {
            pressed = true;
            activate();
        }
    }

    fun activate()
    {
        if(state != "active") {
            actor.anim++;
            sfx.play();
            onActivate();
            state = "active";
        }
    }

    fun deactivate()
    {
        if(state == "active") {
            actor.anim--;
            onDeactivate();
            state = "idle";
        }
    }

    fun isActive()
    {
        return (state == "active");
    }
}