forked from BlenderCN-Org/upbge_random_city_generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
char.py
192 lines (132 loc) · 4.41 KB
/
char.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import bge, json
from bge.logic import globalDict
from random import choice, random
from pprint import pprint
from ast import literal_eval as litev
if not 'player_active' in globalDict.keys():
globalDict['player_active'] = False
def init(cont):
""" Initializes the character. """
own = cont.owner
scene = own.scene
# Sensors
autostart = cont.sensors['autostart'].positive
# Actuators
track_to = cont.actuators['track_to']
# Objects
track_direction = own.childrenRecursive['track_direction']
# Properties
#### INITIALIZE ####
if autostart:
if track_to.object == None:
track_to.object = track_direction
cont.activate(track_to)
own.childrenRecursive['char_mesh'].color[0] = random()
own.childrenRecursive['camera_smooth'].timeOffset = 5
if not globalDict['player_active']:
scene.active_camera = own.childrenRecursive['camera_char']
own['is_player'] = True
globalDict['player_active'] = True
pass
def set_direction(cont):
""" Sets the direction of track object and properties of character. """
own = cont.owner
scene = own.scene
# Sensors
always = cont.sensors['always'].positive
up = cont.sensors['up'].positive
down = cont.sensors['down'].positive
left = cont.sensors['left'].positive
right = cont.sensors['right'].positive
run = cont.sensors['run'].positive
# Actuators
track_to = cont.actuators['track_to']
# Objects
track_direction = own.childrenRecursive['track_direction']
directions = [obj for obj in own.childrenRecursive if obj.name.startswith('dir_')]
if len(directions) > 0:
new_dic = {}
for obj in directions:
new_dic[obj.name] = obj
directions = new_dic
# Properties
#### INITIALIZE ####
if not run:
own['run'] = False
if not up and not down and not left and not right or up and down or left and right:
own['walk'] = False
own['run'] = False
if up and not down or not up and down or left and not right or not left and right:
own['walk'] = True
if run:
own['run'] = True
if up and not down:
if not left and not right:
track_direction.worldPosition = directions['dir_U'].worldPosition
elif left and not right:
track_direction.worldPosition = directions['dir_UL'].worldPosition
elif not left and right:
track_direction.worldPosition = directions['dir_UR'].worldPosition
if not up and down:
if not left and not right:
track_direction.worldPosition = directions['dir_D'].worldPosition
elif left and not right:
track_direction.worldPosition = directions['dir_DL'].worldPosition
elif not left and right:
track_direction.worldPosition = directions['dir_DR'].worldPosition
if not up and not down:
if left and not right:
track_direction.worldPosition = directions['dir_L'].worldPosition
elif not left and right:
track_direction.worldPosition = directions['dir_R'].worldPosition
pass
def mov_anim(cont):
""" Moves the character and animates its armature. """
own = cont.owner
scene = own.scene
# Sensors
autostart = cont.sensors['autostart'].positive
is_walk = cont.sensors['is_walk'].positive
is_run = cont.sensors['is_run'].positive
# Actuators
motion = cont.actuators[0]
# Objects
char_armature = own.childrenRecursive['char_armature']
# Properties
LOOP = bge.logic.KX_ACTION_MODE_LOOP
blend_in = 5
motion_vec = [0, 0, 0]
motion_spd = -0.07
#### INITIALIZE ####
if autostart:
if not is_walk:
char_armature.playAction('character', 0, 120, blendin=blend_in, play_mode=LOOP)
motion_vec[1] = 0
elif is_walk and not is_run:
char_armature.playAction('character', 130, 145, blendin=blend_in, play_mode=LOOP)
motion_vec[1] = motion_spd
elif is_walk and is_run:
char_armature.playAction('character', 150, 165, blendin=blend_in, play_mode=LOOP)
motion_vec[1] = motion_spd * 2
motion.dLoc = motion_vec
cont.activate(motion)
pass
def camera_collision(cont):
""" Avoids the camera to pass through objects. """
own = cont.owner
scene = own.scene
# Sensors
always = cont.sensors['always'].positive
# Objects
camera = own.childrenRecursive['camera_char']
axis = own.childrenRecursive['camera_axis']
origin = own.childrenRecursive['camera_origin']
# Properties
dist = axis.getDistanceTo(origin)
ray = own.rayCast(origin, axis, dist)
#### INITIALIZE ####
if always:
if ray[0] != None:
camera.worldPosition = ray[1]
elif ray[0] == None:
camera.worldPosition = origin.worldPosition