-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.pde
109 lines (92 loc) · 2.71 KB
/
Game.pde
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright 2017 Billy Brown
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
class Game {
private Actor player;
public int dungeonLevel;
public Actor opponent;
private List<Actor> actors = new LinkedList<Actor>();
private Map<Dungeon.Point, List<Item>> itemDrops = new HashMap<Dungeon.Point, List<Item>>();
public Game() {
reset();
}
public void reset() {
player = null;
dungeonLevel = 0;
actors.clear();
itemDrops.clear();
opponent = null;
}
public void drawActors(final PVector offset) {
for (final Actor actor : actors) {
actor.draw(offset);
}
}
public void addActor(final Actor actor) {
actors.add(actor);
}
public void removeActor(final Actor actor) {
actors.remove(actor);
}
public void addItemDrop(final Item item) {
addItemDrop(item, toCell(player.getPosition()));
}
public void addItemDrop(final Item item, final Dungeon.Point point) {
// Fetch the items for the given location
List<Item> items = itemDrops.get(point);
if (items == null) {
// Create the list and associate it if there are none
items = new LinkedList<Item>();
itemDrops.put(point, items);
}
// Add the item to the list for the given location
items.add(item);
}
public Item takeItemDrop(final Item item) {
return takeItemDrop(item, toCell(player.getPosition()));
}
public Item takeItemDrop(final Item item, final Dungeon.Point point) {
List<Item> items = itemDrops.get(point);
if (items != null) {
items.remove(item);
}
return item;
}
public List<Item> itemsAt(final Dungeon.Point point) {
List<Item> items = itemDrops.get(point);
if (items == null) {
// Return an empty list instead of null
items = new LinkedList<Item>();
}
return items;
}
public void setPlayer(final Actor player) {
if (this.player == null) {
this.player = player;
actors.add(player);
}
}
public Actor getPlayer() {
return player;
}
public List<Actor> getActors() {
return actors;
}
public Dungeon.Point toCell(final PVector position) {
return new Dungeon.Point(
(int) floor(position.x) / ExploreState.cellSize,
(int) floor(position.y) / ExploreState.cellSize);
}
public PVector toCoord(final Dungeon.Point point) {
return new PVector(
point.x * ExploreState.cellSize + ExploreState.cellSize / 2,
point.y * ExploreState.cellSize + ExploreState.cellSize / 2);
}
}