-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworld_manager.py
More file actions
70 lines (54 loc) · 2.43 KB
/
world_manager.py
File metadata and controls
70 lines (54 loc) · 2.43 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Manages the world."""
from __future__ import annotations
from random import Random
import numpy as np
from tcod.ecs import Registry
import game.managers.global_manager as global_manager
from game.components import Graphic, Name, Position, Quantity, Tiles
from game.constants import COLOR_BLACK, COLOR_GOLD, COLOR_MAGENTA, GROUND, MAP_HEIGHT, MAP_WIDTH, WATER
from game.managers.log_manager import Log
from game.tags import IsActor, IsIn, IsItem, IsPlayer
def new_world() -> Registry:
"""Generate a new world."""
world = Registry()
world[None].components[Log] = Log()
# Create entities and assign components and tags to them.
# https://python-tcod.readthedocs.io/en/latest/tutorial/part-02.html
map0 = world[object()]
map0.components[Tiles] = GROUND[np.random.randint(GROUND.size, size=(MAP_HEIGHT, MAP_WIDTH))]
global_manager.maps["map0"] = map0
map1 = world[object()]
map1.components[Tiles] = WATER[np.random.randint(WATER.size, size=(MAP_HEIGHT, MAP_WIDTH))]
global_manager.maps["map1"] = map1
player = world[object()]
player.components[Position] = Position(MAP_WIDTH // 2, MAP_HEIGHT // 2)
player.components[Graphic] = Graphic(ord("@"))
player.tags |= {IsPlayer, IsActor}
player.relation_tag[IsIn] = map0
stairs_up = world[object()]
stairs_up.components[Position] = Position(55, 55)
stairs_up.components[Graphic] = Graphic(ord("<"), COLOR_BLACK, COLOR_MAGENTA)
stairs_up.tags.add("up")
stairs_up.relation_tag[IsIn] = map1
stairs_down = world[object()]
stairs_down.components[Position] = Position(58, 58)
stairs_down.components[Graphic] = Graphic(ord(">"), COLOR_BLACK, COLOR_MAGENTA)
stairs_down.tags.add("down")
stairs_down.relation_tag[IsIn] = map0
rng = Random()
for _ in range(50):
gold = world[object()]
gold.components[Position] = Position(rng.randint(0, MAP_WIDTH), rng.randint(0, MAP_HEIGHT))
gold.components[Graphic] = Graphic(ord("$"), COLOR_GOLD)
gold.components[Quantity] = rng.randint(1, 10)
gold.components[Name] = "Gold"
gold.tags |= {IsItem}
gold.relation_tag[IsIn] = map0
scroll = world[object()]
scroll.components[Position] = Position(MAP_WIDTH // 2, MAP_HEIGHT // 2)
scroll.components[Graphic] = Graphic(ord("?"))
scroll.components[Quantity] = 1
scroll.components[Name] = "Scroll"
scroll.tags |= {IsItem}
scroll.relation_tag[IsIn] = map1
return world