fight near (with going)
This commit is contained in:
parent
9f1dd0638e
commit
74cc330573
2 changed files with 139 additions and 33 deletions
93
gui.py
93
gui.py
|
@ -1142,6 +1142,9 @@ def fight(screen, gs):
|
|||
action = gs.fight['user_log'][0]
|
||||
unit = gs.fight['units'][action['unit']]
|
||||
|
||||
if not unit['health'] > 0:
|
||||
del gs.fight['user_log'][0]
|
||||
else:
|
||||
if action['action'] == 'F':
|
||||
unit['fortify'] = True
|
||||
unit['in_action'] = False
|
||||
|
@ -1151,6 +1154,7 @@ def fight(screen, gs):
|
|||
pygame.draw.line(screen, (0, 0, 0), (50, 300), (1130, 300), 3)
|
||||
gs.fight['action'] = 'shooting'
|
||||
elif action['action'] == 'G':
|
||||
|
||||
if unit['pos'] == action['target']:
|
||||
i = (unit['pos'][0] - 52) / 20
|
||||
j = (unit['pos'][1] - 502) / 20
|
||||
|
@ -1160,6 +1164,81 @@ def fight(screen, gs):
|
|||
|
||||
del gs.fight['user_log'][0]
|
||||
|
||||
else:
|
||||
#check is field empty or enemy and empty field is near
|
||||
moving_target = list(action['target'])
|
||||
calculating_road = True
|
||||
while calculating_road:
|
||||
target_unit = gs.get_fight_unit_from_pos(moving_target)
|
||||
if not target_unit:
|
||||
calculating_road = False
|
||||
break
|
||||
|
||||
if abs(moving_target[0] - unit['pos'][0]) > abs(moving_target[1] - unit['pos'][1]):
|
||||
if moving_target[0] > unit['pos'][0]:
|
||||
moving_target[0] -= 20
|
||||
elif moving_target[0] < unit['pos'][0]:
|
||||
moving_target[0] += 20
|
||||
else:
|
||||
if moving_target[1] > unit['pos'][1]:
|
||||
moving_target[1] -= 20
|
||||
elif moving_target[1] < unit['pos'][1]:
|
||||
moving_target[1] += 20
|
||||
|
||||
recheck_unit = gs.get_fight_unit_from_pos(moving_target)
|
||||
if not recheck_unit:
|
||||
calculating_road = False
|
||||
|
||||
if target_unit['country'] != unit['country']:
|
||||
#calculating damage
|
||||
if target_unit['fortify']:
|
||||
fortify = 1
|
||||
else:
|
||||
fortify = 2
|
||||
|
||||
if unit['type'] == 'P':
|
||||
if target_unit['type'] == 'P':
|
||||
target_unit['health'] -= unit['health'] / 4 * fortify
|
||||
unit['health'] -= target_unit['health'] / 4
|
||||
elif target_unit['type'] == 'A':
|
||||
target_unit['health'] -= unit['health'] / 8 * fortify
|
||||
unit['health'] -= target_unit['health'] / 4
|
||||
else:
|
||||
target_unit['health'] -= unit['health'] / 2 * fortify
|
||||
unit['health'] -= target_unit['health'] / 12
|
||||
elif unit['type'] == 'A':
|
||||
if target_unit['type'] == 'P':
|
||||
target_unit['health'] -= unit['health'] / 4 * fortify
|
||||
unit['health'] -= target_unit['health'] / 8
|
||||
elif target_unit['type'] == 'A':
|
||||
target_unit['health'] -= unit['health'] / 4 * fortify
|
||||
unit['health'] -= target_unit['health'] / 4
|
||||
else:
|
||||
target_unit['health'] -= unit['health'] / 2 * fortify
|
||||
unit['health'] -= target_unit['health'] / 12
|
||||
else:
|
||||
if target_unit['type'] == 'P':
|
||||
target_unit['health'] -= unit['health'] / 12 * fortify
|
||||
unit['health'] -= target_unit['health'] / 2
|
||||
elif target_unit['type'] == 'A':
|
||||
target_unit['health'] -= unit['health'] / 12 * fortify
|
||||
unit['health'] -= target_unit['health'] / 2
|
||||
else:
|
||||
target_unit['health'] -= unit['health'] / 4 * fortify
|
||||
unit['health'] -= target_unit['health'] / 4
|
||||
|
||||
if unit['pos'] == tuple(moving_target):
|
||||
calculating_road = False
|
||||
|
||||
|
||||
if unit['pos'] == tuple(moving_target):
|
||||
i = (unit['pos'][0] - 52) / 20
|
||||
j = (unit['pos'][1] - 502) / 20
|
||||
unit['field'] = (i, j)
|
||||
unit['in_action'] = False
|
||||
gs.fight['fields'][i][j] = unit['id']
|
||||
|
||||
del gs.fight['user_log'][0]
|
||||
else:
|
||||
gs.fight['fields'][unit['field'][0]][unit['field'][1]] = ''
|
||||
pygame.draw.rect(screen, (0, 0, 0), (unit['pos'][0] - 1, unit['pos'][1] - 1, 19, 19), 5)
|
||||
|
@ -1169,14 +1248,14 @@ def fight(screen, gs):
|
|||
|
||||
x = unit['pos'][0]
|
||||
y = unit['pos'][1]
|
||||
if x < action['target'][0]:
|
||||
if x < moving_target[0]:
|
||||
x += 1
|
||||
elif x > action['target'][0]:
|
||||
elif x > moving_target[0]:
|
||||
x -= 1
|
||||
|
||||
if y < action['target'][1]:
|
||||
if y < moving_target[1]:
|
||||
y += 1
|
||||
elif y > action['target'][1]:
|
||||
elif y > moving_target[1]:
|
||||
y -= 1
|
||||
|
||||
unit['pos'] = (x, y)
|
||||
|
@ -1200,7 +1279,11 @@ def fight(screen, gs):
|
|||
for unit in gs.fight['units']:
|
||||
distance = int(math.sqrt((unit['pos'][0] - x)**2 + (unit['pos'][1] - y - 10)**2))
|
||||
if longness / distance:
|
||||
unit['health'] -= (longness - distance)
|
||||
fortify = 1
|
||||
if unit['fortify']:
|
||||
fortify = 2
|
||||
|
||||
unit['health'] -= (longness - distance) / fortify
|
||||
|
||||
gs.fight['param_int'] = 0
|
||||
gs.fight['selected_unit'] = None
|
||||
|
|
25
guilib.py
25
guilib.py
|
@ -388,11 +388,34 @@ class GameSettings:
|
|||
for p in border_extended:
|
||||
if p[0] == capital[0] and p[1] == y:
|
||||
count += 1
|
||||
print(count)
|
||||
|
||||
if count > 0 and count % 2 == 1:
|
||||
return border
|
||||
|
||||
def get_fight_unit_from_pos(self, pos):
|
||||
"""Get unit from position in fight mode"""
|
||||
field_pos = self.get_field_from_pos(pos)
|
||||
for unit in self.fight['units']:
|
||||
if unit['health'] > 0 and field_pos == unit['field']:
|
||||
return unit
|
||||
|
||||
return None
|
||||
|
||||
def get_field_from_pos(self, pos):
|
||||
"""Get field from position in fight mode"""
|
||||
start_x = 50
|
||||
start_y = 500
|
||||
for i in range(54):
|
||||
for j in range(9):
|
||||
if start_x < pos[0] < start_x + 20 and start_y < pos[1] < start_y + 20:
|
||||
return (i, j)
|
||||
|
||||
start_y += 20
|
||||
|
||||
start_x += 20
|
||||
start_y = 500
|
||||
|
||||
|
||||
|
||||
class Country:
|
||||
"""Main class for country."""
|
||||
|
|
Reference in a new issue