My build_mode code

mail@pastecode.io avatar
unknown
python
2 months ago
5.6 kB
1
Indexable
Never
#BuildModeHandler.gd
#@tool
#class_name BuildModeHandler
extends Node3D
#docstring

# Load your building scene

#region Variables
#signals
#enums
#constants
#@export variables
#public variables
var foundation = preload("res://foundation.tscn")
var camera
var shifted
var preview
var build_mode : bool = false:
    set(value):
        build_mode = value
        Galy.build_mode = value
        $"../../../UI/VBoxContainer/Build_Mode".visible = value
        $"../../../UI/VBoxContainer/Build_Scale".visible = value
        $"../../../UI/VBoxContainer/Build_Rotation".visible = value
var buildscale: float = 1:
    set(value):
        buildscale = clamp(value,1,10)
        $"../../../UI/VBoxContainer/Build_Scale".text = str("Scale: ", clamp(value,1,10))
        if preview != null:
            preview.scale = Vector3(clamp(value,1,10),clamp(value,1,10),clamp(value,1,10))
var buildrotation: float = 0:
    set(value):
        buildrotation = value + 360 if value < 0 else value if value < 360 else value - 360
        $"../../../UI/VBoxContainer/Build_Rotation".text = str("Rotation: ", buildrotation)
        if preview != null:
            preview.rotation_degrees.y = buildrotation
var scale_mode = true:
    set(value):
        scale_mode = value
        if value:
            $"../../../UI/VBoxContainer/Build_Mode".text = "Build Mode: Scale"
var rotate_mode = false:
    set(value):
        rotate_mode = value
        if value:
            $"../../../UI/VBoxContainer/Build_Mode".text = "Build Mode: Rotate"
#private variables
#@onready variables
#endregion


#region Functions
#optional built-in virtual _init method
#optional built-in virtual _enter_tree() method
#optional built-in virtual _ready method - Called when the node enters the scene tree for the first time.
func _ready():
    pass
#region remaining built-in virtual methods
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    _handle_build_mode()
    
    
func _physics_process(delta):
    handle_build_preview()
    
    
func _input(event):
    if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed and build_mode:
        place_building_at_mouse()
    if event.is_action_pressed("build_mode"):
        build_mode = !build_mode
    if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_WHEEL_UP and build_mode:
        if shifted:
            print("shift-scroll")
#endregion
#region public methods
func place_building_at_mouse():
    var result = raycast_to_mouse()
    if result and not (result.collider.collision_layer & 24) and result.has("position"):
        var foundation = foundation.instantiate()
        foundation.scale = Vector3(buildscale,buildscale,buildscale)
        foundation.rotation_degrees.y = buildrotation
        get_tree().current_scene.add_child(foundation)
        foundation.global_position = result.position
    elif result:
        print("this is a wall or a building")
        
        
func place_preview_at_mouse(result):
    preview = foundation.instantiate()
    preview.get_node("CollisionShape3D").queue_free()
    preview.scale = Vector3(buildscale,buildscale,buildscale)
    preview.rotation_degrees.y = buildrotation
    get_tree().current_scene.add_child(preview)
    preview.global_position = result.position


func move_preview_to_mouse(mouseraypos):
    if mouseraypos.has("position"):
        if !(preview.position == mouseraypos.position):
            preview.position = mouseraypos.position            
        

func raycast_to_mouse():
    var mouse_pos = get_viewport().get_mouse_position()
    camera = Galy.my_cam.camera
    var from = camera.project_ray_origin(mouse_pos)
    var to = from + camera.project_ray_normal(mouse_pos) * 50.0
    var space_state = camera.get_world_3d().direct_space_state
    var rayparams = PhysicsRayQueryParameters3D.create(from, to, 28)
    var result = space_state.intersect_ray(rayparams)
    return result
    
func handle_build_preview():
    var mouseraypos = raycast_to_mouse() if build_mode else null
    if not build_mode or not mouseraypos.has("position") or (mouseraypos.collider.collision_layer & 24):
        if preview:
            preview.queue_free()
            preview = null
    elif preview:
        move_preview_to_mouse(mouseraypos)
    else:
        place_preview_at_mouse(mouseraypos)
#endregion
#region private methods
func _handle_build_mode():
    if !build_mode:
        return
    if Input.is_action_just_pressed("build_mode_switch_mode"):
        scale_mode = !scale_mode
        rotate_mode = !rotate_mode
    if scale_mode:
        if Input.is_action_just_pressed("build_scale_up"):
            buildscale += 1 if Input.is_action_pressed("shift") else 0.1 if Input.is_action_pressed("ctrl") else 0.01
        elif Input.is_action_just_pressed("build_scale_down"):
            buildscale -= 1 if Input.is_action_pressed("shift") else 0.1 if Input.is_action_pressed("ctrl") else 0.01
    elif rotate_mode:
        if Input.is_action_just_pressed("build_scale_up"):
            buildrotation += 10 if Input.is_action_pressed("shift") else 5 if Input.is_action_pressed("ctrl") else 1
        elif Input.is_action_just_pressed("build_scale_down"):
            buildrotation -= 10 if Input.is_action_pressed("shift") else 5 if Input.is_action_pressed("ctrl") else 1
        
#endregion
#region subclasses
#endregion
#endregion

#region Auto_Include_Functions
#endregion



Leave a Comment