-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit.py
118 lines (99 loc) · 4.41 KB
/
unit.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
import logging
from custom_logger_levels import LoggerLevels
# Setup logging
logger = logging.getLogger(__name__)
logger.setLevel(1)
class Unit:
# Class representing the basic unit (bot) and its state
def __init__(self, board, unit_id, player, initial_loc, initial_hp=3):
self.board = board # The board_matrix the unit is on
self.var_data = {} # holds user-defined variable data
# Unit identification
self.id = unit_id
self.player = player
# State variables
self.loc = initial_loc
self.hp = initial_hp
self.spawn_timer = 0
self.charge_timer = 0
self.charge_strength = 0
self.unit_turn_number = 0
self.defending = False
self.performed_critical_action = False # Critical actions are user-commands such as attack() or move(),
# which may not be performed more than once a turn
def set_spawn(self, interval):
# Begin spawn countdown. After interval units have passed and timer reaches 0, a new unit will be spawned.
# In the meantime the unit is unable to act
self.spawn_timer += interval
def on_new_turn(self):
# Reset or update relevant state variables
self.unit_turn_number += 1
self.defending = False
self.performed_critical_action = False
self.decrement_spawn_timer_and_spawn_if_ready()
self.decrement_charge_timer_and_attack_if_ready()
def decrement_spawn_timer_and_spawn_if_ready(self):
# Decrement spawn timer (if active) and spawn new unit when it hits 0, returning the state of the spawn
# (True if spawn succeeded, False if not)
if self.spawn_timer > 0:
self.spawn_timer -= 1
if self.spawn_timer == 0:
return self.board.spawn_in_adjacent_location(self.player, self.loc)
return False
def decrement_hp(self, dmg):
# Reduce hp, check if unit dies (return True if it does, False otherwise - for testing purposes)
self.hp -= dmg
logger.log(LoggerLevels.ActionMessage, "Unit " + str(self.id) + " took " + str(dmg) + " damage")
if self.hp <= 0:
self.kill()
return True
return False
def kill(self):
# Despawn unit from board_matrix
self.board.despawn_unit(self)
logger.log(LoggerLevels.ActionMessage, "Unit " + str(self.id) + " destroyed")
def defend(self):
# Enter defense mode (unit will block next attack against it)
self.defending = True
def attack(self, dmg):
# Attack adjacent enemy for dmg points of damage
self.board.attack_adjacent_enemy(self, dmg)
def charge_attack(self, num_turns):
# Set timer on charge attack, or simply attack if called to wait 0 turns
if num_turns == 0:
self.attack(1)
else:
logger.log(LoggerLevels.ActionMessage, "Unit " + str(self.id) + " is charging an attack!")
self.charge_timer = num_turns
def decrement_charge_timer_and_attack_if_ready(self):
# Decrement charge timer (if active) and attack when it hits 0
if self.charge_timer > 0:
self.charge_timer -= 1
self.charge_strength += 1
if self.charge_timer == 0:
dmg = (self.charge_strength + 1) * (self.charge_strength + 2) / 2
self.attack(int(dmg))
self.charge_strength = 0
self.critical_action_performed()
def damage(self, dmg):
# Unit is under attack for dmg points of damage. Check if unit is defending. If it is, break defense.
# Otherwise, unit hp is decremented
if self.defending:
self.defending = False
logger.log(LoggerLevels.ActionMessage, "Unit " + str(self.id) + " defense broken")
return
self.decrement_hp(dmg)
def fortify(self):
# Fortify command: gain 1 hp
self.hp += 1
def get_turn_number(self):
# Return the number of turns this unit has been alive
return self.unit_turn_number
def can_act(self):
# Verify that unit is able to act (has not performed a critical action this turn, and no timer is active)
return self.spawn_timer == 0 \
and self.charge_timer == 0 \
and self.performed_critical_action is False
def critical_action_performed(self):
# Setter for critical action
self.performed_critical_action = True