-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.py
More file actions
121 lines (112 loc) · 5.49 KB
/
load.py
File metadata and controls
121 lines (112 loc) · 5.49 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
111
112
113
114
115
116
117
118
119
120
121
from pyglet.gl import *
import levelData
from collisionObject import CollisionObject
from shapes import Rectangle
from shapes import Circle
from visualRectangle import VisualRectangle
from entity import Entity
from vector2D import Vector2D
import tileTypeData
from tileType import TileType
from entityType import EntityType
import entityTypeData
currentScreen = ""
gameStart = False
def LoadLevelData(screen):
global gameStart
currentScreen = screen
levelData.collisionObjects.clear()
levelData.backgroundTiles.clear()
levelData.foregroundTiles.clear()
levelData.entities.clear()
scale = levelData.windowWidth / levelData.gridWidth
currentCommand = ""
keywords = ["bgTiles", "fgTiles", "wallTiles", "entity"]
f = open("data/" + currentScreen + ".txt", "r")
for line in f:
pos = None
image = None
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
elif line.split()[0] == "fgTiles":
for word in line.split():
#line format: fgTiles <width> <height>
currentCommand = "fgTiles"
#if line describes wall tiles
elif 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:])
entityType = entityTypeData.GetEntityTypeByID(int(entityData[1]))
entityTypeName = entityType.name
pos = Vector2D(float(entityData[2]), float(entityData[3]))
if entityTypeName == "exit":
#exit entity format: <entity> <entityTypeID> <x> <y> <linked screen> <direction>
#add entity of type exit with a collision object at specified position
exitEntity = Entity(entityType, pos.x, pos.y, True)
exitCollObj = CollisionObject(Rectangle(pos, levelData.tileSize, levelData.tileSize), "exit")
exitCollObj.parentEntity = exitEntity
exitEntity.collisionObj = exitCollObj
exitEntity.SetAttributes(attributes)
levelData.entities.append(exitEntity)
if entityTypeName == "playerStart":
#only set player's starting position if gameStart hasn't been set to true yet
#not an entity
if not gameStart:
levelData.player.position = pos
gameStart = True
if entityTypeName == "mine":
#add entity of type mine with a collision object at specified position
mineEntity = Entity(entityType, pos.x, pos.y, True)
mineEntity.image = entityType.image
mineCollObj = CollisionObject(Circle(pos, 32), "mine")
mineCollObj.parentEntity = mineEntity
mineEntity.collisionObj = mineCollObj
levelData.entities.append(mineEntity)
else:
cellIndex = 0
if currentCommand == "bgTiles":
for word in line.split():
#line format: <tileID> <tileID> <tileID> etc.
pos = Vector2D(cellIndex % levelData.gridWidth, cellIndex // levelData.gridWidth)
pos *= scale
if (int(word)) != 0:
image = tileTypeData.GetTileTypeByID(int(word)).image
levelData.backgroundTiles.append(VisualRectangle(pos, image))
cellIndex += 1
if currentCommand == "fgTiles":
for word in line.split():
#line format: <tileID> <tileID> <tileID> etc.
pos = Vector2D(cellIndex % levelData.gridWidth, cellIndex // levelData.gridWidth)
pos *= scale
if (int(word)) != 0:
image = tileTypeData.GetTileTypeByID(int(word)).image
levelData.foregroundTiles.append(VisualRectangle(pos, image))
cellIndex += 1
if currentCommand == "wallTiles":
for word in line.split():
#line format: <tileID> <tileID> <tileID> etc.
pos = Vector2D(cellIndex % levelData.gridWidth, cellIndex // levelData.gridWidth)
pos *= scale
if (int(word)) != 0:
image = tileTypeData.GetTileTypeByID(int(word)).image
levelData.collisionObjects.append(CollisionObject(Rectangle(pos, levelData.tileSize, levelData.tileSize), "wall"))
levelData.foregroundTiles.append(VisualRectangle(pos, image))
cellIndex += 1
currentCommand = ""
f.close()