From 9baddac840e0f9144a42d7e885783ec835e3e287 Mon Sep 17 00:00:00 2001 From: Michel Date: Thu, 17 Jun 2021 23:03:45 +0200 Subject: [PATCH] Included mouse movement of player char --- project.godot | 5 ++++- scenes/Game.tscn | 13 ++++++++++++- scenes/World.gd | 13 ++++++++++++- scenes/pirate.gd | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 scenes/pirate.gd diff --git a/project.godot b/project.godot index 90acb5f..56fec97 100644 --- a/project.godot +++ b/project.godot @@ -10,7 +10,6 @@ config_version=4 _global_script_classes=[ ] _global_script_class_icons={ - } [application] @@ -31,21 +30,25 @@ view=false walk_left={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":85,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null) ] } walk_right={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null) ] } walk_up={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":86,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null) ] } walk_down={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":73,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null) ] } interact={ diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 27e9edc..20033c6 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://scenes/World.gd" type="Script" id=1] [ext_resource path="res://scenes/HexTile.gd" type="Script" id=2] +[ext_resource path="res://assets/pirate.svg" type="Texture" id=3] +[ext_resource path="res://scenes/pirate.gd" type="Script" id=4] [node name="GameRoot" type="Node"] @@ -75,3 +77,12 @@ margin_top = 13.0 margin_right = 317.0 margin_bottom = 27.0 text = "(0,0)" + +[node name="PlayerChar" type="KinematicBody2D" parent="World"] +script = ExtResource( 4 ) + +[node name="pirate" type="Sprite" parent="World/PlayerChar"] +scale = Vector2( 0.45498, 0.45498 ) +texture = ExtResource( 3 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="World/PlayerChar"] diff --git a/scenes/World.gd b/scenes/World.gd index d744c00..9c4d1db 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -5,20 +5,25 @@ onready var WorldCamera = get_node("Camera") onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue") onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue") onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") +onready var PlayerChar = get_node("PlayerChar") var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() var hex_size = 128 -var hex_grid_bbox = [[0,0], [100,100]] +var hex_grid_bbox = [[0,0], [10,10]] var hex_hover = Vector2.ZERO var is_dragging = false var drag_start = null +var target = Vector2() # Called when the node enters the scene tree for the first time. func _ready(): HexGrid.hex_scale = Vector2(hex_size, hex_size) HexTile.hex_scale = Vector2(hex_size, hex_size) + + # Set player starting position + PlayerChar.position = HexGrid.get_hex_center(Vector2(0,0)) func _draw(): for r in range(hex_grid_bbox[0][0], hex_grid_bbox[1][0]): @@ -44,12 +49,18 @@ func world_to_screen(pos: Vector2): func _unhandled_input(event): if event is InputEventMouseButton: + # Move main character + if event.pressed and event.button_index == BUTTON_LEFT: + PlayerChar.target = HexGrid.get_hex_center(screen_to_hex(event.position)) + + # Move camera if event.pressed and event.button_index == 3: is_dragging = true drag_start = (WorldCamera.offset / WorldCamera.zoom.x + event.position) else: is_dragging = false + # Zoom Camera if event.pressed and event.button_index == BUTTON_WHEEL_DOWN and event.pressed: WorldCamera.zoom = WorldCamera.zoom * 1.0 / 0.8 elif event.button_index == BUTTON_WHEEL_UP and event.pressed: diff --git a/scenes/pirate.gd b/scenes/pirate.gd new file mode 100644 index 0000000..49234b6 --- /dev/null +++ b/scenes/pirate.gd @@ -0,0 +1,32 @@ +extends KinematicBody2D + +export (int) var speed = 200 + +var velocity = Vector2() +var target = Vector2() + +func get_input(): + velocity = Vector2() + if Input.is_action_pressed("walk_right"): + velocity.x += 1 + if Input.is_action_pressed("walk_left"): + velocity.x -= 1 + if Input.is_action_pressed("walk_down"): + velocity.y += 1 + if Input.is_action_pressed("walk_up"): + velocity.y -= 1 + velocity = velocity.normalized() * speed + +func _physics_process(delta): + velocity = position.direction_to(target) * speed + if position.distance_to(target) > 5: + velocity = move_and_slide(velocity) + +# Called when the node enters the scene tree for the first time. +func _ready(): + position = Vector2(0,0) + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass