JScratch
Loading...
Searching...
No Matches
ScratchObject.java
Go to the documentation of this file.
1package com.jscratch;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Map;
6import java.util.concurrent.ConcurrentHashMap;
7import java.util.concurrent.CopyOnWriteArrayList;
8
9public abstract class ScratchObject {
10 protected String name;
11 protected Map<String, Object> variables = new ConcurrentHashMap<>();
12 protected Map<String, List<Object>> lists = new ConcurrentHashMap<>();
13 protected double volume = 100;
14 protected List<Thread> activeScripts = new CopyOnWriteArrayList<>();
15
16 public ScratchObject(String name) {
17 this.name = name;
18 }
19
20 public String getName() {
21 return name;
22 }
23
24 // --- Variables ---
25 public Object getVariable(String name) {
26 if (variables.containsKey(name)) {
27 return variables.get(name);
28 }
29 return 0; // Default
30 }
31
32 public void setVariable(String name, Object value) {
33 variables.put(name, value);
34 }
35
36 public double getVariableAsNumber(String name) {
38 }
39
40 public String getVariableAsString(String name) {
42 }
43
44 public String answer() {
45 return getVariableAsString("answer");
46 }
47
48 public void changeVariableBy(String name, double amount) {
49 Object val = getVariable(name);
50 double current = castToDouble(val);
51 variables.put(name, current + amount);
52 }
53
54 // --- Lists ---
55 public List<Object> getList(String name) {
56 return lists.computeIfAbsent(name, k -> new CopyOnWriteArrayList<>());
57 }
58
59 public void addToList(String listName, Object item) {
60 getList(listName).add(item);
61 }
62
63 public void deleteFromList(String listName, int index) {
64 List<Object> list = getList(listName);
65 if (index >= 1 && index <= list.size()) {
66 list.remove(index - 1);
67 }
68 }
69
70 public Object itemOfList(String listName, int index) {
71 List<Object> list = getList(listName);
72 if (index >= 1 && index <= list.size()) {
73 return list.get(index - 1);
74 }
75 return "";
76 }
77
78 // --- Operators ---
79 public int pickRandom(int min, int max) {
80 return (int) (Math.random() * (max - min + 1)) + min;
81 }
82
83 public String join(Object a, Object b) {
84 return castToString(a) + castToString(b);
85 }
86
87 public String letterOf(int index, String text) {
88 if (index >= 1 && index <= text.length()) {
89 return String.valueOf(text.charAt(index - 1));
90 }
91 return "";
92 }
93
94 public int lengthOf(String text) {
95 return text.length();
96 }
97
98 // --- Helpers ---
99 protected double castToDouble(Object o) {
100 if (o instanceof Number) return ((Number) o).doubleValue();
101 try {
102 return Double.parseDouble(o.toString());
103 } catch (Exception e) {
104 return 0;
105 }
106 }
107
108 protected String castToString(Object o) {
109 return o == null ? "" : o.toString();
110 }
111
112 // --- Script Execution ---
113 public void startScript(Runnable script) {
114 Thread t = new Thread(() -> {
115 try {
116 script.run();
117 } finally {
118 activeScripts.remove(Thread.currentThread());
119 }
120 });
121 activeScripts.add(t);
122 t.start();
123 }
124
125 public void broadcast(String message) {
126 Stage.getInstance().broadcast(message);
127 }
128
129 public void whenIReceive(String message, Runnable action) {
130 Stage.getInstance().whenIReceive(message, action);
131 }
132
133 public void whenKeyPressed(int keyCode, Runnable action) {
134 Stage.getInstance().whenKeyPressed(keyCode, action);
135 }
136
137 public void waitTillNextFrame() {
139 }
140
141 public void wait(double sec) {
142 try {
143 Thread.sleep((long) (sec * 1000));
144 } catch (Exception e) {
145 Thread.currentThread().interrupt();
146 }
147 }
148
149 // --- Audio ---
150 private List<javax.sound.sampled.Clip> activeClips = new java.util.concurrent.CopyOnWriteArrayList<>();
151
152 public void playSound(String soundPath) {
153 new Thread(() -> {
154 try {
155 java.io.File file = new java.io.File(soundPath);
156 if (!file.exists()) return;
157 javax.sound.sampled.AudioInputStream ais = javax.sound.sampled.AudioSystem.getAudioInputStream(file);
158 javax.sound.sampled.Clip clip = javax.sound.sampled.AudioSystem.getClip();
159 clip.open(ais);
160
161 javax.sound.sampled.FloatControl gainControl = (javax.sound.sampled.FloatControl) clip.getControl(javax.sound.sampled.FloatControl.Type.MASTER_GAIN);
162 float dB = (float) (Math.log(volume / 100.0) / Math.log(10.0) * 20.0);
163 if (dB < -80.0f) dB = -80.0f;
164 gainControl.setValue(dB);
165
166 activeClips.add(clip);
167 clip.addLineListener(event -> {
168 if (event.getType() == javax.sound.sampled.LineEvent.Type.STOP) {
169 clip.close();
170 activeClips.remove(clip);
171 }
172 });
173 clip.start();
174 } catch (Exception e) {}
175 }).start();
176 }
177
178 public void stopAllSounds() {
179 for (javax.sound.sampled.Clip clip : activeClips) {
180 clip.stop();
181 clip.close();
182 }
183 activeClips.clear();
184 }
185
186 public void setVolume(double percent) {
187 this.volume = percent;
188 }
189}
Object getVariable(String name)
void changeVariableBy(String name, double amount)
void addToList(String listName, Object item)
double castToDouble(Object o)
String getVariableAsString(String name)
void deleteFromList(String listName, int index)
Map< String, Object > variables
void setVolume(double percent)
void playSound(String soundPath)
int pickRandom(int min, int max)
double getVariableAsNumber(String name)
Object itemOfList(String listName, int index)
Map< String, List< Object > > lists
List< Object > getList(String name)
void broadcast(String message)
void startScript(Runnable script)
List< javax.sound.sampled.Clip > activeClips
String letterOf(int index, String text)
void whenKeyPressed(int keyCode, Runnable action)
void setVariable(String name, Object value)
void whenIReceive(String message, Runnable action)
String join(Object a, Object b)
void waitNextFrame()
Definition Stage.java:136
void broadcast(String message)
Definition Stage.java:96
static Stage getInstance()
Definition Stage.java:325
void whenIReceive(String message, Runnable action)
Definition Stage.java:105
void whenKeyPressed(int keyCode, Runnable action)
Definition Stage.java:109