Compare commits

..

2 Commits

Author SHA1 Message Date
28462f4c0d Controller Support added 2024-12-03 21:01:00 +01:00
af0a145953 Sprengen funktioniert 2024-12-03 20:16:17 +01:00
3 changed files with 336 additions and 51 deletions

56
bomb.gd
View File

@@ -37,10 +37,6 @@ func _process(delta: float) -> void:
if is_off_screen() and not has_exploded: if is_off_screen() and not has_exploded:
trigger_explosion() trigger_explosion()
# Check for spacebar press to explode, only if the bomb hasn't exploded
if Input.is_action_just_pressed("throw_bomb") and not has_exploded:
trigger_explosion()
# This function is used to set the velocity of the bomb when its thrown # This function is used to set the velocity of the bomb when its thrown
func set_velocity(new_velocity: Vector2) -> void: func set_velocity(new_velocity: Vector2) -> void:
# Apply an impulse based on the new velocity # Apply an impulse based on the new velocity
@@ -93,39 +89,41 @@ func push_away_players():
body.apply_central_impulse(impulse) body.apply_central_impulse(impulse)
func destroy_tiles_in_explosion_area(): func destroy_tiles_in_explosion_area():
var center = global_position # This is the center of the explosion in global coordinates var center = global_position # Explosion center in global coordinates
var radius = 30 # The radius of the explosion var radius = 150 # Explosion radius in pixels
# Convert center to map coordinates, taking scale into account # Convert center to map coordinates
var map_center = tile_map_layer.local_to_map(center) var map_center = tile_map_layer.local_to_map(center)
print("Center: ", map_center) print("Explosion center in map coordinates:", center, map_center)
# Get the scale factor of the TileMapLayer # Tile dimensions
var scale = tile_map_layer.scale.x # Assuming uniform scale for both x and y
var tile_width = 16 var tile_width = 16
var tile_height = 16 var tile_height = 16
# Get the bounding box for the explosion area in map coordinates # Calculate bounds of explosion in tile coordinates
var min_x = int((map_center.x - radius * tile_width) / tile_width) var min_x = int((center.x - radius) / tile_width)
var max_x = int((map_center.x + radius * tile_width) / tile_width) var max_x = int((center.x + radius) / tile_width)
var min_y = int((map_center.y - radius * tile_height) / tile_height) var min_y = int((center.y - radius) / tile_height)
var max_y = int((map_center.y + radius * tile_height) / tile_height) var max_y = int((center.y + radius) / tile_height)
# Loop through all cells in the bounding box print("Explosion bounds in tile coordinates: ", Vector2i(min_x, min_y), Vector2i(max_x, max_y))
# Loop through the affected tiles
for x in range(min_x, max_x + 1): for x in range(min_x, max_x + 1):
for y in range(min_y, max_y + 1): for y in range(min_y, max_y + 1):
# Calculate distance from the center of the explosion (map coordinates) # Calculate the distance from the explosion center to the tile center
var dist = map_center.distance_to(Vector2(x, y)) var tile_pos = tile_map_layer.map_to_local(Vector2i(x, y))
var dist = center.distance_to(tile_pos)
# Check if the tile is within the explosion radius (using the distance check)
# Check if the tile is within the explosion radius
if dist <= radius: if dist <= radius:
# Print out some debug information print("Checking tile at:", Vector2i(x, y), "Distance:", dist)
print("Checking tile:", Vector2i(x, y), "Distance:", dist) print(tile_map_layer.get_cell_source_id(Vector2i(x, y)))
# Check if the tile is within the bounds of the map and not empty # Erase the tile if it exists
if tile_map_layer.get_cell_source_id(Vector2i(x, y)) != -1: if tile_map_layer.get_cell_source_id(Vector2i(x, y)) != -1:
# Erase the tile at coordinates (x, y) in map coordinates
tile_map_layer.erase_cell(Vector2i(x, y)) tile_map_layer.erase_cell(Vector2i(x, y))
print("Erased tile at:", Vector2i(x, y))
# Function to check if the bomb is off-screen # Function to check if the bomb is off-screen

281
main.tscn

File diff suppressed because one or more lines are too long

View File

@@ -12,14 +12,19 @@ extends RigidBody2D
@export var friction = 5 @export var friction = 5
@export var max_speed = 500.0 @export var max_speed = 500.0
@export var player_index = 0 # Default to keyboard (-1). Set to controller ID for multiplayer.
var charging = false var charging = false
var current_power = 0.0 var current_power = 0.0
var direction = Vector2(2, -2) # Default direction, can be modified for aiming
var bomb = null # Global bomb reference var bomb = null # Global bomb reference
var aim_direction = Vector2(1, -1) # Default aiming direction
@onready var velocity_label = $Label @onready var velocity_label = $Label
@onready var line_indicator = $Line2D # Reference to the Line2D node @onready var line_indicator = $Line2D # Reference to the Line2D node
# Dead zone for joystick input
var dead_zone = 0.2
func _ready() -> void: func _ready() -> void:
# Set the initial points for the Line2D to indicate the starting vector # Set the initial points for the Line2D to indicate the starting vector
line_indicator.clear_points() line_indicator.clear_points()
@@ -29,53 +34,68 @@ func _ready() -> void:
func _process(delta: float) -> void: func _process(delta: float) -> void:
if position.y > 2000: if position.y > 2000:
get_tree().reload_current_scene() get_tree().reload_current_scene()
# Start charging when "throw_bomb" is pressed
if Input.is_action_pressed("throw_bomb") and (!bomb or !is_instance_valid(bomb)): update_aim_direction() # Continuously update aiming direction
# Start charging when "throw_bomb" is pressed (either button or key)
if is_throwing_input_pressed() and (!bomb or !is_instance_valid(bomb)):
charging = true charging = true
current_power += charge_rate * delta current_power += charge_rate * delta
current_power = min(current_power, max_power) # Cap power at max_power current_power = min(current_power, max_power) # Cap power at max_power
# Update velocity on the label # Update velocity on the label
velocity_label.text = "Power: " + str(current_power) velocity_label.text = "Power: " + str(current_power)
# Calculate the direction based on the input (for example, using the mouse position or joystick)
var aim_direction = (get_global_mouse_position() - global_position).normalized()
# Set the start position of the line a bit away from the player # Set the start position of the line a bit away from the player
var start_position = aim_direction * line_start_offset var start_position = aim_direction * line_start_offset
# Update the Line2D to visualize the throw direction and power # Update the Line2D to visualize the throw direction and power
var end_point = start_position + (aim_direction * current_power / 10) var end_point = start_position + (aim_direction * current_power / 10)
#line_indicator.set_point_position(1, end_point) # Set the ending point of the vector
line_indicator.set_point_position(0, start_position) # Set the starting point of the vector line_indicator.set_point_position(0, start_position) # Set the starting point of the vector
line_indicator.set_point_position(1, end_point) # Set the ending point of the vector line_indicator.set_point_position(1, end_point) # Set the ending point of the vector
elif Input.is_action_just_released("throw_bomb") and charging: elif is_throwing_input_pressed() and is_instance_valid(bomb):
bomb.trigger_explosion()
elif not is_throwing_input_pressed() and charging:
# Throw bomb when the action is released # Throw bomb when the action is released
throw_bomb(current_power)
charging = false charging = false
throw_bomb(current_power)
current_power = 0.0 # Reset power current_power = 0.0 # Reset power
# Reset Line2D to initial state after release # Reset Line2D to initial state after release
line_indicator.set_point_position(0, Vector2.ZERO) line_indicator.set_point_position(0, Vector2.ZERO)
line_indicator.set_point_position(1, Vector2.ZERO) line_indicator.set_point_position(1, Vector2.ZERO)
func update_aim_direction():
if player_index == -1: # Keyboard/Mouse
aim_direction = (get_global_mouse_position() - global_position).normalized()
else: # Controller
var joy_x = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_X)
var joy_y = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_Y)
# Apply dead zone
if abs(joy_x) > dead_zone or abs(joy_y) > dead_zone:
aim_direction = Vector2(joy_x, joy_y).normalized()
func is_throwing_input_pressed() -> bool:
if player_index == -1: # Keyboard/Mouse
return Input.is_action_pressed("throw_bomb")
else: # Controller (use A button for throwing)
return Input.is_joy_button_pressed(player_index, JOY_BUTTON_A)
func throw_bomb(power: float): func throw_bomb(power: float):
# Check if there is no bomb or if the current bomb instance is invalid # Check if there is no bomb or if the current bomb instance is invalid
if !bomb or !is_instance_valid(bomb): if !bomb or !is_instance_valid(bomb):
# Instantiate the bomb # Instantiate the bomb
bomb = Bomb.instantiate() # Use global bomb reference bomb = Bomb.instantiate() # Use global bomb reference
bomb.position = global_position bomb.position = global_position
# Calculate the direction based on the mouse position
var aim_direction = (get_global_mouse_position() - global_position).normalized()
# Set the velocity for the bomb: direction * power # Set the velocity for the bomb: direction * power
var launch_velocity = aim_direction * power * 2 var launch_velocity = aim_direction * power * 2
bomb.set_velocity(launch_velocity) bomb.set_velocity(launch_velocity)
# Add the bomb to the scene # Add the bomb to the scene
add_child(bomb) add_child(bomb)
# Clear velocity display after throwing (optional) # Clear velocity display after throwing (optional)
velocity_label.text = "Power: 0" velocity_label.text = "Power: 0"