JScratch
Loading...
Searching...
No Matches
Scroller2d.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.util.*;
7
8/*
9 *
10 *
11 * CREDIT TO GEMINI CLI
12 *
13 * NOT TO GURTAJ
14 *
15 *
16 *
17 *
18 */
19
20
21public class Scroller2d {
22 /* slot start [id:"Scroller2d_members" tags:"inClassNotMethod"] */
23 private static Scroller2d instance;
24 public int x, y = 0;
25 private List<BSprite> registeredSprites = new ArrayList<>();
26 private List<BackgroundTile> tiles = new ArrayList<>();
27
28 public static final int TILE_WIDTH = 480;
29 public static final int TILE_HEIGHT = 360;
30
31 public static Scroller2d getInstance() {
32 if (instance != null) {
33 return instance;
34 } else {
35 instance = new Scroller2d();
36 return instance;
37 }
38 }
39
40 public void register(BSprite s) {
41 if (!registeredSprites.contains(s)) {
42 registeredSprites.add(s);
43 }
44 }
45
46 public Scroller2d() {
47 /* slot start [id:"Scroller2d_init" tags:"inInstanceMethod"] */
48 Stage stage = Stage.getInstance();
49
50 // Initialize 9 tiles (3x3 grid)
51 for (int i = 0; i < 9; i++) {
52 BackgroundTile tile = new BackgroundTile("Tile" + i);
53
54 // Add costumes for nearby tiles
55 // IMPORTANT: Use (240, 180) as the center for a 480x360 tile
56 for (int tx = -1; tx <= 1; tx++) {
57 for (int ty = -1; ty <= 1; ty++) {
58 tile.addCostume(new Costume(tx + "," + ty, "jscratch_ws/assets/" + tx + "," + ty + ".png", 240, 180));
59 }
60 }
61 tile.addCostume(new Costume("err", "jscratch_ws/assets/error.png", 240, 180));
62
63 tiles.add(tile);
64 stage.addSprite(tile);
65 // Tiles should be at the back
66 stage.goToBack(tile);
67 }
68
69 update();
70 /* slot end [id:"Scroller2d_init"] */
71 }
72
73 public void update() {
74 // Calculate the world coordinates of the tile the camera is currently over.
75 // Rounding to nearest tile ensures the 3x3 grid stays roughly centered.
76 int tcx = (int) Math.round((double)-x / TILE_WIDTH);
77 int tcy = (int) Math.round((double)-y / TILE_HEIGHT);
78
79 int tileIdx = 0;
80 // Arrange 9 tiles in a 3x3 grid centered around (tcx, tcy)
81 for (int dy = 1; dy >= -1; dy--) {
82 for (int dx = -1; dx <= 1; dx++) {
83 int worldX = tcx + dx;
84 int worldY = tcy + dy;
85
86 BackgroundTile tile = tiles.get(tileIdx++);
87
88 // Screen Position = (World Position * Tile Size) + Camera Offset
89 // Since we used (240, 180) as the costume center, goTo(0,0) centers it on stage.
90 int screenX = (worldX * TILE_WIDTH) + x;
91 int screenY = (worldY * TILE_HEIGHT) + y;
92
93 tile.superGoTo(screenX, screenY);
94 tile.switchCostume(worldX + "," + worldY);
95 }
96 }
97
98
99 for (BSprite s : registeredSprites) {
100 s.update();
101 }
102 }
103 /* slot end [id:"Scroller2d_members"] */
104}
void switchCostume(String n)
Definition Sprite.java:221
void addCostume(Costume c)
Definition Sprite.java:240
void addSprite(Sprite s)
Definition Stage.java:145
static Stage getInstance()
Definition Stage.java:337
void goToBack(Sprite s)
Definition Stage.java:156
void superGoTo(int x, int y)
static Scroller2d getInstance()
List< BSprite > registeredSprites
static final int TILE_HEIGHT
static final int TILE_WIDTH
static Scroller2d instance
void register(BSprite s)
List< BackgroundTile > tiles