11package org .openpatch .scratch ;
22
3- import java .util .ArrayList ;
3+ import java .util .List ;
44import java .util .concurrent .ConcurrentHashMap ;
55import java .util .concurrent .CopyOnWriteArrayList ;
66
1616import org .openpatch .scratch .internal .Drawable ;
1717import org .openpatch .scratch .internal .Image ;
1818import org .openpatch .scratch .internal .Sound ;
19+ import org .openpatch .scratch .internal .Stamp ;
1920
2021import processing .event .KeyEvent ;
2122import processing .event .MouseEvent ;
@@ -36,6 +37,7 @@ public class Sprite implements Drawable {
3637 private final ConcurrentHashMap <String , Timer > timer ;
3738 private final Pen pen ;
3839 private Hitbox hitbox ;
40+ private boolean hitboxDisabled = false ;
3941 private final Text text ;
4042
4143 public Sprite () {
@@ -78,6 +80,8 @@ public Sprite(Sprite s) {
7880 this .pen = new Pen (s .pen );
7981 this .text = new Text (s .text );
8082 this .stage = s .stage ;
83+ this .hitbox = s .hitbox ;
84+ this .hitboxDisabled = s .hitboxDisabled ;
8185 }
8286
8387 public void addedToStage (Stage stage ) {
@@ -475,6 +479,9 @@ public void setOnEdgeBounce(boolean b) {
475479 }
476480
477481 public void ifOnEdgeBounce () {
482+ if (this .hitboxDisabled )
483+ return ;
484+
478485 var h = this .getHitbox ();
479486
480487 if (h .intersects (this .stage .leftBorder )) {
@@ -798,6 +805,8 @@ public void removeTimer(String name) {
798805 * @return true if touching
799806 */
800807 public boolean isTouchingMousePointer () {
808+ if (this .hitboxDisabled )
809+ return false ;
801810
802811 var bounds = this .getHitbox ().getPolygon ().getBounds ();
803812 double topLeftCornerX = bounds .getMinX ();
@@ -849,6 +858,8 @@ public boolean isTouchingMousePointer() {
849858 * @return true if outside
850859 */
851860 public boolean isTouchingEdge () {
861+ if (this .hitboxDisabled )
862+ return false ;
852863 var h = this .getHitbox ();
853864 return h .intersects (this .stage .topBorder ) || h .intersects (this .stage .bottomBorder )
854865 || h .intersects (this .stage .leftBorder ) || h .intersects (this .stage .rightBorder );
@@ -889,6 +900,14 @@ public void setHitbox(Hitbox hitbox) {
889900 this .hitbox = hitbox ;
890901 }
891902
903+ public void disableHitbox () {
904+ this .hitboxDisabled = true ;
905+ }
906+
907+ public void enableHitbox () {
908+ this .hitboxDisabled = false ;
909+ }
910+
892911 public Hitbox getHitbox () {
893912 Image currentCostume = null ;
894913 if (this .costumes .size () > this .getCurrentCostumeIndex ()) {
@@ -955,37 +974,23 @@ public Hitbox getHitbox() {
955974 }
956975
957976 public boolean isTouchingSprite (Sprite sprite ) {
958- if (sprite == null || !sprite .show )
977+ if (sprite == null || !sprite .show || sprite . hitboxDisabled )
959978 return false ;
960979 return this .getHitbox ().intersects (sprite .getHitbox ());
961980 }
962981
963982 public boolean isTouchingSprite (Class <? extends Sprite > c ) {
964- for (Drawable d : this .stage .drawables ) {
965- if (c .isInstance (d ) && this .isTouchingSprite ((Sprite ) d )) {
966- return true ;
967- }
968- }
969- return false ;
983+ return this .stage .sprites .stream ().filter (s -> c .isInstance (s ) && this .isTouchingSprite (s )).findFirst ().isPresent ();
970984 }
971985
972- public Sprite getTouchingSprite (Class <? extends Sprite > c ) {
973- for (Drawable d : this .stage .drawables ) {
974- if (c .isInstance (d ) && this .isTouchingSprite ((Sprite ) d )) {
975- return (Sprite ) d ;
976- }
977- }
978- return null ;
986+ public <T extends Sprite > T getTouchingSprite (Class <T > c ) {
987+ return (T ) this .stage .sprites .stream ().filter (s -> c .isInstance (s ) && this .isTouchingSprite (s )).findFirst ()
988+ .orElse (null );
979989 }
980990
981- public ArrayList <Sprite > getTouchingSprites (Class <? extends Sprite > c ) {
982- ArrayList <Sprite > sprites = new ArrayList <>();
983- for (Drawable d : this .stage .drawables ) {
984- if (c .isInstance (d ) && this .isTouchingSprite ((Sprite ) d )) {
985- sprites .add ((Sprite ) d );
986- }
987- }
988- return sprites ;
991+ public <T extends Sprite > List <T > getTouchingSprites (Class <T > c ) {
992+ return (List <T >) this .stage .sprites .stream ().filter (s -> c .isInstance (s ) && this .isTouchingSprite (s ))
993+ .toList ();
989994 }
990995
991996 /**
@@ -1212,16 +1217,25 @@ public void broadcast(String message) {
12121217 public void whenIReceive (String message ) {
12131218 }
12141219
1215- public void stamp () {
1220+ public void stampToBackground () {
12161221 if (this .costumes .size () > 0 ) {
1217- this .costumes .get (this .currentCostume )
1218- .draw (
1219- this .size ,
1220- this .direction ,
1221- this .x ,
1222- this .y ,
1223- this .rotationStyle ,
1224- true );
1222+ var stamp = new Stamp (this .costumes .get (this .currentCostume ),
1223+ this .direction ,
1224+ this .x ,
1225+ this .y ,
1226+ this .rotationStyle );
1227+ this .stage .backgroundStamps .add (stamp );
1228+ }
1229+ }
1230+
1231+ public void stampToForeground () {
1232+ if (this .costumes .size () > 0 ) {
1233+ var stamp = new Stamp (this .costumes .get (this .currentCostume ),
1234+ this .direction ,
1235+ this .x ,
1236+ this .y ,
1237+ this .rotationStyle );
1238+ this .stage .foregroundStamps .add (stamp );
12251239 }
12261240 }
12271241
@@ -1236,7 +1250,9 @@ public void draw() {
12361250 }
12371251
12381252 if (Applet .getInstance ().isDebug ()) {
1239- this .getHitbox ().draw ();
1253+ if (!this .hitboxDisabled ) {
1254+ this .getHitbox ().draw ();
1255+ }
12401256 }
12411257 this .text .setPosition (
12421258 this .x + this .getWidth () * 0.9 / 2 ,
0 commit comments