-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveAndLoad.py
More file actions
110 lines (99 loc) · 4.14 KB
/
saveAndLoad.py
File metadata and controls
110 lines (99 loc) · 4.14 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
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
110
import screenData
import tileTypeData
import entityTypeData
from entity import Entity
selectedScreenFilename = ""
#pre-defined level data, muliple layers with the tile IDs for each tile in those layers - read from levelData.txt
def LoadLevelData():
currentCommand = ""
keywords = ["bgTiles", "fgTiles", "wallTiles", "entity"]
screenData.ClearAllLayers()
f = open(selectedScreenFilename, "r")
for line in f:
if line.split()[0] in keywords:
#if line describes background tiles
if line.split()[0] == "bgTiles":
for word in line.split():
#line format: bgTiles <width> <height>
currentCommand = "bgTiles"
#if line describes foreground tiles
if line.split()[0] == "fgTiles":
for word in line.split():
#line format: fgTiles <width> <height>
currentCommand = "fgTiles"
#if line describes wall tiles
if line.split()[0] == "wallTiles":
for word in line.split():
#line format: wallTiles <width> <height>
currentCommand = "wallTiles"
#if line describes entity
if line.split()[0] == "entity":
entityData = []
for word in line.split():
#line format: <entity> <entityTypeID> <x> <y> <attributes>
currentCommand = "entity"
entityData.append(word)
attributes = ""
if len(entityData) > 4:
attributes = " ".join(entityData[4:])
screenData.entities.append(Entity(entityTypeData.GetEntityTypeByID(int(entityData[1])), float(entityData[2]), float(entityData[3]), False, attributes))
else:
if currentCommand == "bgTiles":
for i in range(len(line.split())):
word = line.split()[i]
#line format: <tileID> <tileID> <tileID> etc.
screenData.bgLayer[i] = tileTypeData.GetTileTypeByID(int(word))
currentCommand = ""
if currentCommand == "fgTiles":
for i in range(len(line.split())):
word = line.split()[i]
#line format: <tileID> <tileID> <tileID> etc.
screenData.fgLayer[i] = tileTypeData.GetTileTypeByID(int(word))
currentCommand = ""
if currentCommand == "wallTiles":
for i in range(len(line.split())):
word = line.split()[i]
#line format: <tileID> <tileID> <tileID> etc.
screenData.wallsLayer[i] = tileTypeData.GetTileTypeByID(int(word))
currentCommand = ""
f.close()
def SaveData():
f = open(selectedScreenFilename, "w")
lines = []
#add tile IDs for the background, foreground, and walls layers
lines.append("bgTiles ")
lines[0] = lines[0] + str(screenData.gridWidth) + " " + str(screenData.gridHeight) + " "
lines.append("")
for tile in screenData.bgLayer:
id = ""
if tile is not None:
id = str(tile.id)
else:
id = "0"
lines[1] = lines[1] + id + " "
lines.append("fgTiles ")
lines[2] = lines[2] + str(screenData.gridWidth) + " " + str(screenData.gridHeight) + " "
lines.append("")
for tile in screenData.fgLayer:
id = ""
if tile is not None:
id = str(tile.id)
else:
id = "0"
lines[3] = lines[3] + id + " "
lines.append("wallTiles ")
lines[4] = lines[4] + str(screenData.gridWidth) + " " + str(screenData.gridHeight) + " "
lines.append("")
for tile in screenData.wallsLayer:
id = ""
if tile is not None:
id = str(tile.id)
else:
id = "0"
lines[5] = lines[5] + id + " "
for entity in screenData.entities:
lines.append("entity " + str(entity.entityType.id) + " " + str(entity.x) + " " + str(entity.y) + " " + entity.attributes)
for line in lines:
f.write(line)
f.write('\n')
f.close()