forked from croza/RR2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitHandler.py
156 lines (119 loc) · 5.05 KB
/
unitHandler.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
from direct.actor.Actor import Actor
from direct.task.Task import *
from direct.showbase.DirectObject import DirectObject
from panda3d.ai import *
import random, sys, os, math, copy
class world:
def __init__(self, unitDict, tileList):
self.AIWorld = AIWorld(render)
self.cTrav = CollisionTraverser('world') # TBC
self.cTrav.showCollisions(render)
base.cTrav = self.cTrav
# self.cTrav.traverse(render)
self.unitDict = unitDict # The dictionary created of all the units and their keys by the parser
self.gameUnits = {} # A dictionary containing all the units in the game, with each unit's ID as its key
self.unitMoving = {} # Shows whether a unit is moving or not
self.unitUniqueID = 0
taskMgr.add(self.updateWorld, "World task")
def loadWalls(self, unitNumber, tileList):
unit = self.gameUnits[unitNumber]
for row in tileList:
for tile in row:
print tile.name, tile.solid
if (tile.solid == True):
print tile.model
unit.AIbehaviors.addStaticObstacle(tile.model)
def reloadWallsAll(self, tileList):
for unitKey in self.gameUnits:
if (self.unitMoving[unitKey] == 0):
self.gameUnits[unitKey].AIbehaviors.removeAi("all")
#self.gameUnits[unitKey].AIbehaviors.initPathFind("data/models/game/navmesh2.csv")
for row in tileList:
for tile in row:
if (tile.solid == True):
print tile.model
# self.gameUnits[unitKey].AIbehaviors.addStaticObstacle(tile.model)
## pass
def updateWorld(self, task):
self.AIWorld.update()
# print self.gameUnits[0].AIbehaviors.behaviorStatus("pathfollow"), self.gameUnits[0].actor.getPos()
for unit in self.gameUnits:
if (self.gameUnits[unit].AIbehaviors.behaviorStatus("pathfollow") != "active"):
self.unitMoving[unit] = 0
# else:
# self.unitMoving[unit] = 0
if (self.unitMoving[unit] != 0):
self.unitTask(unit)
return Task.cont
def unitTask(self, unitUniqueID):
# Now check for collisions.
self.cTrav.traverse(render)
# Adjust ralph's Z coordinate. If ralph's ray hit terrain,
# update his Z. If it hit anything else, or didn't hit anything, put
# him back where he was last frame.
#print(self.ralphGroundHandler.getNumEntries())
entries = []
for i in range(self.gameUnits[unitUniqueID].actor.groundHandler.getNumEntries()):
entry = self.gameUnits[unitUniqueID].actor.groundHandler.getEntry(i)
entries.append(entry)
entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
x.getSurfacePoint(render).getZ()))
if (len(entries)>0) and (entries[0].getIntoNode().getName() == "Tile"):
self.gameUnits[unitUniqueID].actor.setZ(entries[0].getSurfacePoint(render).getZ())
# else:
# self.gameUnits[unitUniqueID].AIbehaviors.pauseAi("pathfollow")
## print len(entries), entries[0].getIntoNode().getName()
# print 'hi'
self.gameUnits[unitUniqueID].actor.setP(0)
def moveUnit(self, unitUniqueID, target):
print unitUniqueID, target
def roundCoords(coord):
coord2 = list(coord)
for i in range(2):
coord2[i] = float(int(coord2[i]))
coord = tuple(coord2)
return coord
self.gameUnits[unitUniqueID].AIbehaviors.pauseAi("pathfollow")
try:
self.gameUnits[unitUniqueID].AIbehaviors.pathFindTo(target, "addPath")
except:
self.gameUnits[unitUniqueID].actor.setPos(roundCoords(self.gameUnits[unitUniqueID].actor.getPos()))
self.gameUnits[unitUniqueID].AIbehaviors.pathFindTo(target, "addPath")
self.unitMoving[unitUniqueID] = target
class unit:
def addNewModel(self, unitNumber, position, world, tileList):
unit = copy.copy(world.unitDict[unitNumber]) # gets the data from the dictionary of the units
unit.ID = world.unitUniqueID
world.unitUniqueID += 1
unit.actor = Actor(unit.model)
unit.actor.reparentTo(render)
unit.actor.setPos(position)
unit.actor.setCollideMask(0x2)
#unit.cTrav = CollisionTraverser()
unit.actor.groundRay = CollisionRay() # Makes a collisionNode for the unit, to set it to the right height.
unit.actor.groundRay.setOrigin(0,0,1000)
unit.actor.groundRay.setDirection(0,0,-1)
unit.actor.groundCol = CollisionNode('UnitRay')
unit.actor.groundCol.addSolid(unit.actor.groundRay)
unit.actor.groundCol.setFromCollideMask(BitMask32.bit(0))
unit.actor.groundCol.setIntoCollideMask(BitMask32.allOff())
unit.actor.groundColNP = unit.actor.attachNewNode(unit.actor.groundCol)
unit.actor.groundHandler = CollisionHandlerQueue()
world.cTrav.addCollider(unit.actor.groundColNP, unit.actor.groundHandler)
unit.actor.groundColNP.show()
# world.cTrav.showCollisions(render)
unit.AIChar = AICharacter(unit.name,
unit.actor,
2*unit.mass,
2*unit.startforce,
2*unit.maxforce)
# self.world = world
world.AIWorld.addAiChar(unit.AIChar)
unit.AIbehaviors = unit.AIChar.getAiBehaviors()
unit.AIbehaviors.initPathFind("data/models/game/navmesh2.csv")
world.gameUnits[unit.ID] = unit
world.unitMoving[unit.ID] = 0
world.loadWalls(unit.ID, tileList)
print world.gameUnits