2023-09-27 18:03:37 +00:00
|
|
|
import pygame
|
2023-11-13 12:34:22 +00:00
|
|
|
from random import randint, choice
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2024-02-10 17:11:28 +00:00
|
|
|
from config.game.spell_config import magic_data
|
|
|
|
from config.game.weapon_config import weapon_data
|
2024-04-23 18:43:39 +00:00
|
|
|
#
|
2023-09-27 18:03:37 +00:00
|
|
|
from .movement import MovementHandler
|
|
|
|
from .combat import CombatHandler
|
|
|
|
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
class InputHandler(MovementHandler, CombatHandler):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
MovementHandler.__init__(self)
|
|
|
|
CombatHandler.__init__(self)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-10-04 02:37:28 +00:00
|
|
|
self.status = 'down'
|
2023-09-27 18:03:37 +00:00
|
|
|
|
|
|
|
# Setup Movement
|
|
|
|
self.move_cooldown = 15
|
|
|
|
self.can_move = True
|
|
|
|
self.move_time = None
|
|
|
|
|
|
|
|
# Setup Combat
|
|
|
|
self.attacking = False
|
|
|
|
self.attack_cooldown = 400
|
|
|
|
self.attack_time = None
|
|
|
|
|
|
|
|
# Setup Special Actions
|
|
|
|
self.can_rotate_weapon = True
|
|
|
|
self.weapon_rotation_time = None
|
|
|
|
self.rotate_attack_cooldown = 600
|
|
|
|
|
|
|
|
self.can_swap_magic = True
|
|
|
|
self.magic_swap_time = None
|
|
|
|
|
2023-11-13 18:09:41 +00:00
|
|
|
# Setup Action Space
|
2024-04-23 18:43:39 +00:00
|
|
|
self.possible_actions = [0, 1, 2, 3, 4]
|
2023-11-13 18:09:41 +00:00
|
|
|
self.action = 10
|
|
|
|
|
2023-11-29 10:53:30 +00:00
|
|
|
def check_input(self,
|
|
|
|
button,
|
|
|
|
speed,
|
|
|
|
hitbox,
|
|
|
|
obstacle_sprites,
|
2024-04-23 18:43:39 +00:00
|
|
|
rect
|
|
|
|
):
|
2023-11-13 18:09:41 +00:00
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
if not self.attacking and self.can_move:
|
2023-10-04 02:37:28 +00:00
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
self.move_time = pygame.time.get_ticks()
|
|
|
|
|
|
|
|
# Movement Input
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.action == 0: # keys[pygame.K_w]:
|
|
|
|
self.direction.y = -1
|
2023-09-27 18:03:37 +00:00
|
|
|
self.status = 'up'
|
|
|
|
self.can_move = False
|
2023-11-13 18:09:41 +00:00
|
|
|
self.action = 0
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
elif self.action == 1: # keys[pygame.K_s]:
|
|
|
|
self.direction.y = 1
|
2023-09-27 18:03:37 +00:00
|
|
|
self.status = 'down'
|
|
|
|
self.can_move = False
|
2023-11-13 18:09:41 +00:00
|
|
|
self.action = 1
|
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
else:
|
2024-04-23 18:43:39 +00:00
|
|
|
self.direction.y = 0
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.action == 2: # keys[pygame.K_a]:
|
|
|
|
self.direction.x = -1
|
2023-09-27 18:03:37 +00:00
|
|
|
self.status = 'left'
|
|
|
|
self.can_move = False
|
2023-11-13 18:09:41 +00:00
|
|
|
self.action = 2
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
elif self.action == 3: # keys[pygame.K_d]:
|
|
|
|
self.direction.x = 1
|
2023-09-27 18:03:37 +00:00
|
|
|
self.status = 'right'
|
|
|
|
self.can_move = False
|
2023-11-13 18:09:41 +00:00
|
|
|
self.action = 3
|
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
else:
|
2024-04-23 18:43:39 +00:00
|
|
|
self.direction.x = 0
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
self.move(speed, hitbox, obstacle_sprites, rect)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-11-22 00:08:09 +00:00
|
|
|
# Combat Input
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.action == 4 and not self.attacking: # keys[pygame.K_e]
|
2023-11-22 00:08:09 +00:00
|
|
|
self.attacking = True
|
|
|
|
self.attack_time = pygame.time.get_ticks()
|
2024-04-23 18:43:39 +00:00
|
|
|
self.create_attack_sprite()
|
2023-11-22 00:08:09 +00:00
|
|
|
self.action = 4
|
|
|
|
|
|
|
|
# Magic Input
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.action == 5:
|
2023-11-22 00:08:09 +00:00
|
|
|
self.attacking = True
|
|
|
|
self.attack_time = pygame.time.get_ticks()
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
self.magic = list(magic_data.keys())[
|
|
|
|
self.magic_index]
|
2023-11-22 00:08:09 +00:00
|
|
|
|
|
|
|
strength = list(magic_data.values())[
|
2024-04-23 18:43:39 +00:00
|
|
|
self.magic_index]['strength'] + self.stats['magic']
|
2023-11-22 00:08:09 +00:00
|
|
|
|
|
|
|
cost = list(magic_data.values())[
|
2024-04-23 18:43:39 +00:00
|
|
|
self.magic_index]['cost']
|
|
|
|
self.create_magic_sprite(
|
|
|
|
self.magic, strength, cost)
|
2023-11-22 00:08:09 +00:00
|
|
|
self.action = 5
|
|
|
|
|
|
|
|
# Rotating Weapons
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.action == 6 and self.can_rotate_weapon:
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2023-11-22 00:08:09 +00:00
|
|
|
self.can_rotate_weapon = False
|
|
|
|
self.weapon_rotation_time = pygame.time.get_ticks()
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.weapon_index\
|
2023-11-29 10:53:30 +00:00
|
|
|
< len(list(weapon_data.keys())) - 1:
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
self.weapon_index += 1
|
2023-11-22 00:08:09 +00:00
|
|
|
else:
|
2024-04-23 18:43:39 +00:00
|
|
|
self.weapon_index = 0
|
2023-11-22 00:08:09 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
self.weapon = list(weapon_data.keys())[
|
|
|
|
self.weapon_index]
|
2023-11-22 00:08:09 +00:00
|
|
|
self.action = 6
|
|
|
|
|
|
|
|
# Swap Spells
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.action == 7 and self.can_swap_magic:
|
2023-11-22 00:08:09 +00:00
|
|
|
self.can_swap_magic = False
|
|
|
|
self.magic_swap_time = pygame.time.get_ticks()
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.magic_index < len(list(magic_data.keys())) - 1:
|
|
|
|
self.magic_index += 1
|
2023-11-22 00:08:09 +00:00
|
|
|
else:
|
2024-04-23 18:43:39 +00:00
|
|
|
self.magic_index = 0
|
2023-11-22 00:08:09 +00:00
|
|
|
self.action = 7
|
2023-09-27 18:03:37 +00:00
|
|
|
|
|
|
|
def cooldowns(self, vulnerable):
|
|
|
|
current_time = pygame.time.get_ticks()
|
2023-10-04 02:37:28 +00:00
|
|
|
self.vulnerable = vulnerable
|
2023-09-27 18:03:37 +00:00
|
|
|
|
|
|
|
if self.attacking:
|
2023-11-29 10:53:30 +00:00
|
|
|
if current_time - self.attack_time\
|
|
|
|
> self.attack_cooldown\
|
2024-04-23 18:43:39 +00:00
|
|
|
+ weapon_data[self.weapon]['cooldown']:
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
self.attacking = False
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.current_attack:
|
|
|
|
self.delete_attack_sprite()
|
2023-09-27 18:03:37 +00:00
|
|
|
|
|
|
|
if not self.can_rotate_weapon:
|
2023-11-29 10:53:30 +00:00
|
|
|
if current_time - self.weapon_rotation_time\
|
|
|
|
> self.rotate_attack_cooldown:
|
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
self.can_rotate_weapon = True
|
|
|
|
|
|
|
|
if not self.can_swap_magic:
|
2023-11-29 10:53:30 +00:00
|
|
|
if current_time - self.magic_swap_time\
|
|
|
|
> self.rotate_attack_cooldown:
|
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
self.can_swap_magic = True
|
|
|
|
|
|
|
|
if not vulnerable:
|
2024-04-23 18:43:39 +00:00
|
|
|
if current_time - self.hurt_time\
|
|
|
|
>= self.invulnerability_duration:
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
self.vulnerable = True
|
2023-09-27 18:03:37 +00:00
|
|
|
|
|
|
|
if not self.can_move:
|
2023-11-29 10:53:30 +00:00
|
|
|
if current_time - self.move_time\
|
|
|
|
>= self.move_cooldown:
|
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
self.can_move = True
|