Skip to content

Commit

Permalink
Highlighting improvements and no more drag code.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisl8 committed Mar 16, 2024
1 parent 17c1e32 commit 6b9b5d3
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 84 deletions.
2 changes: 2 additions & 0 deletions items/disc/disc.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends RigidBody2D

@export var spawn_position: Vector2
@export var width_in_tiles: int = 2
@export var height_in_tiles: int = 2


func _ready() -> void:
Expand Down
6 changes: 4 additions & 2 deletions items/soup_machine/soup_machine.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
extends RigidBody2D

var item_type: String = "Entity"

@export var spawn_position: Vector2
@export var width_in_tiles: int = 4
@export var height_in_tiles: int = 4

var item_type: String = "Entity"


func _ready() -> void:
Expand Down
Binary file removed items/soup_machine/soup_machine_2.png
Binary file not shown.
34 changes: 0 additions & 34 deletions items/soup_machine/soup_machine_2.png.import

This file was deleted.

6 changes: 4 additions & 2 deletions items/square/square.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
extends RigidBody2D

var item_type: String = "Entity"

@export var spawn_position: Vector2
@export var width_in_tiles: int = 2
@export var height_in_tiles: int = 2

var item_type: String = "Entity"


func _ready() -> void:
Expand Down
50 changes: 35 additions & 15 deletions map/map_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -700,25 +700,37 @@ func _get_drawing_canvas(canvas_entry_name: String = "default") -> Node2D:


func highlight_cell_at_global_position(
at_position: Vector2, color: Color = Color.GREEN, canvas_entry_name: String = "default"
at_position: Vector2,
color: Color = Color.GREEN,
canvas_entry_name: String = "default",
offsetX: int = 8,
offsetY: int = 8,
sizeX: int = 16,
sizeY: int = 16
) -> void:
var drawing_canvas: Node2D = _get_drawing_canvas(canvas_entry_name)
var at_cell_position: Vector2i = get_cell_position_at_global_position(at_position)
var cell_global_position: Vector2 = get_global_position_at_map_local_position(at_cell_position)
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - 8, cell_global_position.y - 8, 16, 16)] = {
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)] = {
}
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - 8, cell_global_position.y - 8, 16, 16)].color = color
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)].color = color
drawing_canvas.update_draw = true


func highlight_cell_at_map_position(
at_cell_position: Vector2i, color: Color = Color.GREEN, canvas_entry_name: String = "default"
at_cell_position: Vector2i,
color: Color = Color.GREEN,
canvas_entry_name: String = "default",
offsetX: int = 8,
offsetY: int = 8,
sizeX: int = 16,
sizeY: int = 16
) -> void:
var drawing_canvas: Node2D = _get_drawing_canvas(canvas_entry_name)
var cell_global_position: Vector2 = get_global_position_at_map_local_position(at_cell_position)
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - 8, cell_global_position.y - 8, 16, 16)] = {
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)] = {
}
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - 8, cell_global_position.y - 8, 16, 16)].color = color
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)].color = color
drawing_canvas.update_draw = true


Expand Down Expand Up @@ -887,32 +899,40 @@ func save_map() -> void:
)


func check_tile_location_and_surroundings(at_position: Vector2i) -> Globals.MapTileSet:
func check_tile_location_and_surroundings(
at_position: Vector2i, height_in_tiles: int = 1, width_in_tiles: int = 1
) -> Globals.MapTileSet:
var half_tile_offset_position: Vector2i = Vector2i(at_position.x + 8, at_position.y + 8)
var cell_position_at_position: Vector2i = (
Globals.world_map.get_cell_position_at_global_position(at_position)
Globals.world_map.get_cell_position_at_global_position(half_tile_offset_position)
)

var return_data: Globals.MapTileSet = Globals.MapTileSet.new()
return_data.all_tiles_are_empty = true

# Find out what tiles exist at the requested position
for x_position: int in range(0, 1):
for y_position: int in range(0, 1):
for x_position: int in range(0, width_in_tiles):
for y_position: int in range(0, height_in_tiles):
return_data.tile_list.append(
Vector2i(
cell_position_at_position.x + x_position,
cell_position_at_position.y + y_position
cell_position_at_position.x - (width_in_tiles / 2) + x_position,
cell_position_at_position.y - (height_in_tiles / 2) + y_position
)
)

Globals.world_map.erase_drawing_canvas("checking_tile_position")
for cell_position: Vector2i in return_data.tile_list:
return_data.tile_content[cell_position] = (
Globals.world_map.get_cell_id_at_map_tile_position(cell_position)
)
if return_data.tile_content[cell_position] != Vector2i(-1, -1):
return_data.all_tiles_are_empty = false
# Globals.world_map.highlight_cell_at_map_position(cell_position, Color.PINK) # For visualizing to debug
# else:
# Globals.world_map.highlight_cell_at_map_position(cell_position, Color.LIME_GREEN) # For visualizing to debug
Globals.world_map.highlight_cell_at_map_position(
cell_position, Color(1.0, 0.0, 0.0, 0.1), "checking_tile_position"
) # For visualizing to debug
else:
Globals.world_map.highlight_cell_at_map_position(
cell_position, Color(0.0, 1.0, 0.0, 0.1), "checking_tile_position"
) # For visualizing to debug

return return_data
5 changes: 3 additions & 2 deletions map/map_drawing.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var update_draw: bool = false

var temp: bool = false


func _process(_delta: float) -> void:
if (
update_draw
Expand Down Expand Up @@ -37,8 +38,8 @@ func _draw() -> void:
if lines_to_draw[key].has("color"):
color = lines_to_draw[key].color
draw_line(lines_to_draw[key].from, lines_to_draw[key].to, color, line_width)
if(temp):
if temp:
rectangles_to_draw.clear()
lines_to_draw.clear()
#queue_redraw()
temp = false
temp = false
37 changes: 8 additions & 29 deletions player/player_interaction_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,7 @@ func _process(delta: float) -> void:
# Placed items are placed when you click the left mouse button.
if controlled_item_type == "Held":
if is_mining:
#controlled_item.set_position(to_local(mouse_position))
#controlled_item.set_position(controlled_item.global_position)
#if(randf() > 0.99):
# controlled_item.set_position(to_local(mouse_position))

#if(is_local):

#controlled_item.add_constant_central_force(controlled_item.global_position-to_local(mouse_position))
#print(to_local(mouse_position) - controlled_item.global_position)
#print(controlled_item.global_position.distance_to(mouse_position))
#print(controlled_item.global_position,mouse_position,(mouse_position - controlled_item.global_position))

var LocationA = mouse_position
var LocationB = controlled_item.to_global(controlled_item.center_of_mass)

Globals.world_map.draw_temp_line_on_map(LocationA, LocationB, Color.CYAN)
var max_force = 50000.0
var force = (LocationA - LocationB) * 25000.0
var mass = controlled_item.mass
var acceleration = force.length() / mass
if acceleration > 100.0:
force = mass * acceleration
if force.length() > max_force:
force = (LocationA - LocationB).normalized() * 50000.0

controlled_item.constant_force = (force)
#controlled_item.x
#controlled_item.apply_central_force ((controlled_item.global_position-to_local(mouse_position)).normalized()*100)
controlled_item.set_position(to_local(mouse_position))
pass

elif is_multiplayer_authority():
Expand All @@ -135,7 +108,13 @@ func _process(delta: float) -> void:
else:
controlled_item.set_position(to_local(mouse_position))
var intersecting_tiles: Globals.MapTileSet = (
Globals.world_map.check_tile_location_and_surroundings(mouse_position)
Globals
. world_map
. check_tile_location_and_surroundings(
mouse_position,
controlled_item.height_in_tiles,
controlled_item.width_in_tiles
)
)
controlled_item_clear_of_collisions = intersecting_tiles.all_tiles_are_empty
Globals.world_map.erase_drawing_canvas(controlled_item.name)
Expand Down

0 comments on commit 6b9b5d3

Please sign in to comment.