pneuma-pygame/game/entities/components/_input.py

145 lines
5.2 KiB
Python
Raw Normal View History

import pygame
2023-11-13 12:34:22 +00:00
from random import randint, choice
from configs.game.spell_config import magic_data
from configs.game.weapon_config import weapon_data
from .movement import MovementHandler
from .combat import CombatHandler
class InputHandler:
2023-10-04 02:37:28 +00:00
# , status):
2023-11-13 12:34:22 +00:00
def __init__(self, sprite_type, animation_player, ai_controller=False):
2023-10-04 02:37:28 +00:00
self.status = 'down'
self.sprite_type = sprite_type
# Setup Movement
2023-10-04 02:37:28 +00:00
self.movement = MovementHandler(self.sprite_type)
self.move_cooldown = 15
self.can_move = True
self.move_time = None
# Setup Combat
2023-10-04 02:37:28 +00:00
self.combat = CombatHandler(animation_player)
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
def check_input(self, speed, hitbox, obstacle_sprites, rect, player):
if not self.attacking and self.can_move:
2023-10-04 02:37:28 +00:00
keys = pygame.key.get_pressed()
2023-10-04 02:37:28 +00:00
2023-11-13 12:34:22 +00:00
button = randint(0, 4)
self.move_time = pygame.time.get_ticks()
# Movement Input
2023-11-13 12:34:22 +00:00
if button == 0: # keys[pygame.K_w]:
self.movement.direction.y = -1
self.status = 'up'
self.can_move = False
2023-11-13 12:34:22 +00:00
elif button == 1: # keys[pygame.K_s]:
self.movement.direction.y = 1
self.status = 'down'
self.can_move = False
else:
self.movement.direction.y = 0
2023-11-13 12:34:22 +00:00
if button == 2: # keys[pygame.K_a]:
self.movement.direction.x = -1
self.status = 'left'
self.can_move = False
2023-11-13 12:34:22 +00:00
elif button == 3: # keys[pygame.K_d]:
self.movement.direction.x = 1
self.status = 'right'
self.can_move = False
else:
self.movement.direction.x = 0
self.movement.move(speed, hitbox, obstacle_sprites, rect)
2023-10-04 02:37:28 +00:00
if self.sprite_type == 'player':
# Combat Input
2023-11-13 12:34:22 +00:00
if button == 4 and not self.attacking: # keys[pygame.K_e]
2023-10-04 02:37:28 +00:00
self.attacking = True
self.attack_time = pygame.time.get_ticks()
self.combat.create_attack_sprite(player)
self.combat.weapon_attack_sound.play()
# Magic Input
2023-11-13 12:34:22 +00:00
if button == 5: # keys[pygame.K_q]:
2023-10-04 02:37:28 +00:00
self.attacking = True
self.attack_time = pygame.time.get_ticks()
self.combat.magic = list(magic_data.keys())[
self.combat.magic_index]
strength = list(magic_data.values())[
self.combat.magic_index]['strength'] + player.stats.magic
cost = list(magic_data.values())[
self.combat.magic_index]['cost']
self.combat.create_magic_sprite(
player, self.combat.magic, strength, cost)
# Rotating Weapons
# keys[pygame.K_LSHIFT]
2023-11-13 12:34:22 +00:00
if button == 6 and self.can_rotate_weapon:
2023-10-04 02:37:28 +00:00
self.can_rotate_weapon = False
self.weapon_rotation_time = pygame.time.get_ticks()
if self.combat.weapon_index < len(list(weapon_data.keys())) - 1:
self.combat.weapon_index += 1
else:
self.combat.weapon_index = 0
self.combat.weapon = list(weapon_data.keys())[
self.combat.weapon_index]
# Swap Spells
# keys[pygame.K_LCTRL] :
2023-11-13 12:34:22 +00:00
if button == 7 and self.can_swap_magic:
2023-10-04 02:37:28 +00:00
self.can_swap_magic = False
self.magic_swap_time = pygame.time.get_ticks()
if self.combat.magic_index < len(list(magic_data.keys())) - 1:
self.combat.magic_index += 1
else:
self.combat.magic_index = 0
def cooldowns(self, vulnerable):
current_time = pygame.time.get_ticks()
2023-10-04 02:37:28 +00:00
self.vulnerable = vulnerable
if self.attacking:
if current_time - self.attack_time > self.attack_cooldown + weapon_data[self.combat.weapon]['cooldown']:
self.attacking = False
if self.combat.current_attack:
self.combat.delete_attack_sprite()
if not self.can_rotate_weapon:
if current_time - self.weapon_rotation_time > self.rotate_attack_cooldown:
self.can_rotate_weapon = True
if not self.can_swap_magic:
if current_time - self.magic_swap_time > self.rotate_attack_cooldown:
self.can_swap_magic = True
if not vulnerable:
2023-10-04 02:37:28 +00:00
if current_time - self.combat.hurt_time >= self.combat.invulnerability_duration:
self.combat.vulnerable = True
if not self.can_move:
if current_time - self.move_time >= self.move_cooldown:
self.can_move = True