JScratch
Loading...
Searching...
No Matches
BSprite.java
Go to the documentation of this file.
1package generated;
2
3import com.jscratch.*;
4import java.awt.Color;
5import java.awt.event.KeyEvent;
6import java.lang.reflect.Field;
7import java.util.*;
8
9public class BSprite extends Sprite {
10 /* slot start [id:"BSprite_members" tags:"inClassNotMethod"] */
11
12
13 public int xx = 0;
14 public int yy = 0;
15 public int scale = 1;
16
17 public BSprite(String name) {
18 super(name);
19
20 // Example: addCostume(new Costume("costume1", "path/to/img.png"));
21 this.whenScaleChanged((old, newscale) -> {
22 double scfa = newscale / (old+0.0);
23 scale = newscale.intValue();
24 this.setScaleX(this.getScaleX() * scfa);
25 this.setScaleY(this.getScaleY() * scfa);
26 update();
27 });
28
30 /* slot start [id:"BSprite_init" tags:"inInstanceMethod"] */
31 /* slot end [id:"BSprite_init"] */
32
33 }
34
35
36 public void update() {
38 super.goTo((xx + scroller.x) * scale, (yy + scroller.y) * scale);
39
40 // Culling: Hide if too far off stage (assuming 480x360 base coords)
41// if (isOnStage()) {
42// show();
43// } else {
44// hide();
45// }
46 }
47 public void goTo(int x, int y) {
48 xx = x;
49 yy = y;
50 update();
51 }
52
53 public void changeY(int y) {
54 yy += y;
55 update();
56 }
57
58 public void changeX(int x) {
59 xx += x;
60 update();
61 }
62 public void walk(int steps) {
63 super.move(steps * scale);
64 }
65
66 public void goToFront() {
68 }
69
70 public void goToBack() {
72 }
73
74 public boolean isDirEqualTo(int dir) {
75 double olddir = (Double) getVariable("direction");
76 int dir2 = (int) olddir;
77
78 return dir2 == dir;
79 }
80 public void animateTurn(int dir, double time) {
81
82 double olddir = (Double) getVariable("direction");
83 double change = (dir - olddir + 540) % 360 - 180;
84
85 if (change > 0) {
86 double timm = time / change;
87 int tim = (int) (timm * 1000);
88 for (int i = 0; i < change; i++) {
89 turnRight(1);
90 try {
91 Thread.sleep(tim);
92 } catch (InterruptedException e) {
93
94 }
95 }
96 } if (change == 0) {
97 return;
98 } else {
99 double p = -change;
100 double timm = time / p;
101 int tim = (int) (timm * 1000);
102 for (int i = 0; i < p; i++) {
103 turnLeft(1);
104 try {
105 Thread.sleep(tim);
106 } catch (InterruptedException e) {
107
108 }
109 }
110 }
111 }
112
113 public void animateTurn(int dir, double time, String curveType) {
114 double olddir = (Double) getVariable("direction");
115 double change = (dir - olddir + 540) % 360 - 180;
116
117
118 if (change == 0) return;
119
120
121 int steps = (int) Math.abs(change);
122 boolean right = change > 0;
123
124 for (int i = 0; i < steps; i++) {
125 double t1 = (double) i / steps;
126 double t2 = (double) (i + 1) / steps;
127
128 double c1 = applyCurve(t1, curveType);
129 double c2 = applyCurve(t2, curveType);
130
131
132 double delta = (c2 - c1) * time;
133
134
135 if (right) {
136 turnRight(1);
137 } else {
138 turnLeft(1);
139 }
140
141
142 try {
143 Thread.sleep((int)(delta * 1000));
144 } catch (InterruptedException e) {
145 e.printStackTrace();
146 }
147 }
148 }
149
150
151 private double applyCurve(double t, String type) {
152 switch (type) {
153 case "easeIn": return t * t;
154 case "easeOut": return 1 - Math.pow(1 - t, 2);
155 case "easeInOut":
156 return t < 0.5
157 ? 2 * t * t
158 : 1 - Math.pow(-2 * t + 2, 2) / 2;
159 default: return t; // linear
160 }
161}
162
163 public boolean isOnStage() {
164 double halfW = width() / 2.0;
165 halfW /= scale + 0.0;
166 double halfH = height() / 2.0;
167 halfH /= scale + 0.0;
168
169
170 double left = xx - halfW;
171 double right = xx + halfW;
172 double bottom = yy - halfH;
173 double top = yy + halfH;
174
175 return right >= -240 &&
176 left <= 240 &&
177 top >= -180 &&
178 bottom <= 180;
179}
180
181 @SuppressWarnings("unchecked")
182 public void resetCostumes() {
183 try {
184 // Access and clear the costumes list
185 Field costumesField = Sprite.class.getDeclaredField("costumes");
186 costumesField.setAccessible(true);
187 List<?> costumes = (List<?>) costumesField.get(this);
188 costumes.clear();
189
190 // Reset costumeIndex to 0
191 Field indexField = Sprite.class.getDeclaredField("costumeIndex");
192 indexField.setAccessible(true);
193 indexField.setInt(this, 0);
194
195 } catch (NoSuchFieldException | IllegalAccessException e) {
196 throw new RuntimeException("Failed to reset costumes via reflection", e);
197 }
198 }
199 /* slot end [id:"BSprite_members"] */
200}
List< Costume > costumes
Definition Sprite.java:26
void turnRight(double deg)
Definition Sprite.java:176
double getScaleY()
Definition Sprite.java:57
double getScaleX()
Definition Sprite.java:56
Sprite(String name)
Definition Sprite.java:38
void setScaleY(double sy)
Definition Sprite.java:163
void whenScaleChanged(java.util.function.BiConsumer< Double, Double > action)
Definition Sprite.java:65
void setScaleX(double sx)
Definition Sprite.java:162
Object getVariable(String name)
Definition Sprite.java:255
void turnLeft(double deg)
Definition Sprite.java:177
static Stage getInstance()
Definition Stage.java:337
void goToBack(Sprite s)
Definition Stage.java:156
void goToFront(Sprite s)
Definition Stage.java:150
void animateTurn(int dir, double time, String curveType)
Definition BSprite.java:113
void changeX(int x)
Definition BSprite.java:58
void animateTurn(int dir, double time)
Definition BSprite.java:80
void walk(int steps)
Definition BSprite.java:62
boolean isDirEqualTo(int dir)
Definition BSprite.java:74
void goTo(int x, int y)
Definition BSprite.java:47
double applyCurve(double t, String type)
Definition BSprite.java:151
void changeY(int y)
Definition BSprite.java:53
boolean isOnStage()
Definition BSprite.java:163
BSprite(String name)
Definition BSprite.java:17
static Scroller2d getInstance()
void register(BSprite s)