-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dwarf.java
82 lines (72 loc) · 2.46 KB
/
Dwarf.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The main character
*
* @author Sandro Lenz, Daniel Fankhauser
* @version v1.2
*/
public class Dwarf extends Actor {
GreenfootImage imgFacingRight = new GreenfootImage("dwarf-right.png");
GreenfootImage imgFacingLeft = new GreenfootImage("dwarf-left.png");
public void act() {
checkKeyPress();
scanBlocks();
}
// Check if WASD or Arrow Keys are pressed and move dwarf
private void checkKeyPress() {
if (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")) {
setLocation(getX(), getY()-4);
checkCollision("up");
}
if (Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("down")) {
setLocation(getX(), getY()+4);
checkCollision("down");
}
if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")) {
setImage(imgFacingLeft);
setLocation(getX()-4, getY());
checkCollision("left");
}
if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")) {
setImage(imgFacingRight);
setLocation(getX()+4, getY());
checkCollision("right");
}
}
private boolean checkCollision(String direction) {
// Check if Dwarf is touching dirt that is not mined and move dwarf back
if(isTouching(Dirt.class)) {
switch(direction) {
case "up":
setLocation(getX(), getY()+8);
break;
case "down":
setLocation(getX(), getY()-8);
break;
case "left":
setLocation(getX()+8, getY());
break;
case "right":
setLocation(getX()-8, getY());
break;
default:
return false;
}
return false;
}
// Check if Dwarf is touching stone
if(isTouching(Stone.class)) {
setLocation(getX(), getY()-4);
return false;
}
return true; // true means no collision
}
// Loop through blocks in range to show where ores are
private void scanBlocks() {
for (Dirt dirt : getObjectsInRange(150, Dirt.class)) {
if(dirt.isOre) {
dirt.showOre();
}
}
}
}