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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// -----------------------------------------------------------------------------
// File: item_creator.ss
// Description: item creator plugin (Debug Mode)
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------
/*
This plugin lets the user put items on the level.
Properties:
- mode: string. One of the following: "mobile", "mouse".
*/
using SurgeEngine;
using SurgeEngine.Actor;
using SurgeEngine.Level;
using SurgeEngine.Player;
using SurgeEngine.Vector2;
using SurgeEngine.Transform;
using SurgeEngine.UI.Text;
using SurgeEngine.Input.Mouse;
using SurgeEngine.Camera;
object "Debug Mode - Item Creator" is "debug-mode-plugin", "private", "awake", "entity"
{
transform = Transform();
input = spawn("Debug Mode - Input");
itemType = "";
itemName = "";
itemPreview = spawn("Debug Mode - Item Creator - Preview");
camera = null; // camera of the debug mode (plugin)
gridSystem = null;
itemPickerHeight = 0;
state "main"
{
}
state "mobile"
{
// follow the camera
transform.position = camera.position;
// create the item if the action button is pressed
if(input.buttonPressed("action")) {
createItem(transform.position);
}
}
state "mouse"
{
// follow the mouse
transform.position = gridSystem.snapToGrid(Camera.screenToWorld(Mouse.position));
// create the item when the user taps the screen
// onTapScreen() ...
}
fun onTapScreen(position)
{
if(state == "mouse") {
if(position.y >= itemPickerHeight)
createItem(transform.position);
}
}
fun createItem(position)
{
if(itemType == "entity") {
entityName = itemName;
Level.spawnEntity(entityName, position);
}
}
fun onLoad(debugMode)
{
uiSettings = debugMode.plugin("Debug Mode - UI Settings");
itemPreview.zindex = uiSettings.zindex + 1000;
itemPicker = debugMode.plugin("Debug Mode - Item Picker");
itemPicker.subscribe(this);
itemPickerHeight = itemPicker.height;
tapDetector = debugMode.plugin("Debug Mode - Tap Detector");
tapDetector.subscribe(this);
camera = debugMode.plugin("Debug Mode - Camera");
gridSystem = debugMode.plugin("Debug Mode - Grid System");
}
fun onUnload(debugMode)
{
tapDetector = debugMode.plugin("Debug Mode - Tap Detector");
tapDetector.unsubscribe(this);
itemPicker = debugMode.plugin("Debug Mode - Item Picker");
itemPicker.unsubscribe(this);
}
fun onPickItem(item)
{
itemType = item.type;
itemName = item.name;
if(itemType == "entity")
itemPreview.setEntity(itemName);
else
itemPreview.setNull();
}
fun constructor()
{
if(SurgeEngine.mobile)
state = "mobile";
else
state = "mouse";
}
}
object "Debug Mode - Item Creator - Preview" is "private", "awake", "entity"
{
public zindex = 0.0;
transform = Transform();
text = Text("GoodNeighbors");
actor = null;
state "main"
{
xpos = Math.floor(transform.position.x);
ypos = Math.floor(transform.position.y);
text.text = xpos + " , " + ypos;
text.zindex = zindex;
}
fun setEntity(entityName)
{
if(actor !== null)
actor.destroy();
actor = Actor(entityName);
actor.visible = true;
actor.alpha = 0.67;
actor.zindex = zindex;
}
fun setNull()
{
if(actor !== null)
actor.destroy();
actor = null;
}
fun constructor()
{
text.align = "center";
text.offset = Vector2(0, 16);
}
}
|