-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCursor.java
More file actions
50 lines (45 loc) · 1.46 KB
/
Copy pathCursor.java
File metadata and controls
50 lines (45 loc) · 1.46 KB
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Cursor is basically the mouse and is used for easier detection of the mouse over a Tile.
* Not really helpful for much else.
*
* @author Angela Wang
* @version June 4 2025
*/
public class Cursor extends Actor
{
private GreenfootImage image;
private MouseInfo mouse;
private int prevX, prevY, x, y;
/**
* Cursor constructor
* @param show True to show green square where Cursor is, false otherwise
*/
public Cursor(boolean show){
image = new GreenfootImage(5, 5);
image.setColor(Color.GREEN);
if (show) image.fill();
setImage(image);
}
/**
* Act - do whatever the Cursor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
//get mouse information
MouseInfo mouse = Greenfoot.getMouseInfo();
//if mouse is in world bounds, set cursor to location
if (mouse != null){
setLocation(mouse.getX(), mouse.getY());
}
}
/**
* Returns ArrayList of Actors the cursor is hovering over. Code by Neelan Thurairajah
* @return ArrayList<Actor> Hovered actors if available, null if not
*/
public ArrayList<Actor> getHoveredActors() {
return (ArrayList<Actor>) getObjectsAtOffset(0, 0, Actor.class);
}
}