Included mouse movement of player char

master
Michel 2021-06-17 23:03:45 +02:00
parent 5e102c1640
commit 9baddac840
4 changed files with 60 additions and 3 deletions

View File

@ -10,7 +10,6 @@ config_version=4
_global_script_classes=[ ] _global_script_classes=[ ]
_global_script_class_icons={ _global_script_class_icons={
} }
[application] [application]
@ -31,21 +30,25 @@ view=false
walk_left={ walk_left={
"deadzone": 0.5, "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) "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={ walk_right={
"deadzone": 0.5, "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) "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={ walk_up={
"deadzone": 0.5, "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) "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={ walk_down={
"deadzone": 0.5, "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) "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={ interact={

View File

@ -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/World.gd" type="Script" id=1]
[ext_resource path="res://scenes/HexTile.gd" type="Script" id=2] [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"] [node name="GameRoot" type="Node"]
@ -75,3 +77,12 @@ margin_top = 13.0
margin_right = 317.0 margin_right = 317.0
margin_bottom = 27.0 margin_bottom = 27.0
text = "(0,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"]

View File

@ -5,21 +5,26 @@ onready var WorldCamera = get_node("Camera")
onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue") onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue")
onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue") onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue")
onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue")
onready var PlayerChar = get_node("PlayerChar")
var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new()
var hex_size = 128 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 hex_hover = Vector2.ZERO
var is_dragging = false var is_dragging = false
var drag_start = null var drag_start = null
var target = Vector2()
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
HexGrid.hex_scale = Vector2(hex_size, hex_size) HexGrid.hex_scale = Vector2(hex_size, hex_size)
HexTile.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(): func _draw():
for r in range(hex_grid_bbox[0][0], hex_grid_bbox[1][0]): for r in range(hex_grid_bbox[0][0], hex_grid_bbox[1][0]):
for c in range(hex_grid_bbox[0][1], hex_grid_bbox[1][1]): for c in range(hex_grid_bbox[0][1], hex_grid_bbox[1][1]):
@ -44,12 +49,18 @@ func world_to_screen(pos: Vector2):
func _unhandled_input(event): func _unhandled_input(event):
if event is InputEventMouseButton: 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: if event.pressed and event.button_index == 3:
is_dragging = true is_dragging = true
drag_start = (WorldCamera.offset / WorldCamera.zoom.x + event.position) drag_start = (WorldCamera.offset / WorldCamera.zoom.x + event.position)
else: else:
is_dragging = false is_dragging = false
# Zoom Camera
if event.pressed and event.button_index == BUTTON_WHEEL_DOWN and event.pressed: if event.pressed and event.button_index == BUTTON_WHEEL_DOWN and event.pressed:
WorldCamera.zoom = WorldCamera.zoom * 1.0 / 0.8 WorldCamera.zoom = WorldCamera.zoom * 1.0 / 0.8
elif event.button_index == BUTTON_WHEEL_UP and event.pressed: elif event.button_index == BUTTON_WHEEL_UP and event.pressed:

32
scenes/pirate.gd Normal file
View File

@ -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