basic player movement
This commit is contained in:
		
							
								
								
									
										26
									
								
								Player/player.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								Player/player.gd
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| extends CharacterBody2D | ||||
|  | ||||
| @export var MAX_SPEED = 600.0; | ||||
| @export var ACCELERATION = 2000.0; | ||||
| @export var FRICTION = 3000.0; | ||||
|  | ||||
| func _physics_process(delta): | ||||
| 	var input_vector = get_input_vector(); | ||||
| 	apply_movement(input_vector, delta); | ||||
| 	apply_friction(input_vector, delta); | ||||
| 	move_and_slide(); | ||||
|  | ||||
| func get_input_vector(): | ||||
| 	var input_vector = Vector2.ZERO; | ||||
| 	input_vector.x = Input.get_action_strength("move_east") - Input.get_action_strength("move_west"); | ||||
| 	input_vector.y = Input.get_action_strength("move_south") - Input.get_action_strength("move_north"); | ||||
| 	return input_vector.normalized(); | ||||
|  | ||||
| func apply_movement(input_vector, delta): | ||||
| 	if(input_vector != Vector2.ZERO): | ||||
| 		velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta); | ||||
| 	 | ||||
|  | ||||
| func apply_friction(input_vector,delta): | ||||
| 	if(input_vector == Vector2.ZERO): | ||||
| 		velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta); | ||||
							
								
								
									
										27
									
								
								Player/player.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								Player/player.tscn
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| [gd_scene load_steps=5 format=3 uid="uid://b62h0w8gjm6tj"] | ||||
|  | ||||
| [ext_resource type="Script" path="res://Player/player.gd" id="1_mbyc2"] | ||||
| [ext_resource type="Texture2D" uid="uid://ca2eovotfh6v2" path="res://placeholder.png" id="2_26hmp"] | ||||
|  | ||||
| [sub_resource type="SpriteFrames" id="SpriteFrames_mqbrd"] | ||||
| animations = [{ | ||||
| "frames": [{ | ||||
| "duration": 1.0, | ||||
| "texture": ExtResource("2_26hmp") | ||||
| }], | ||||
| "loop": true, | ||||
| "name": &"default", | ||||
| "speed": 5.0 | ||||
| }] | ||||
|  | ||||
| [sub_resource type="RectangleShape2D" id="RectangleShape2D_ng5bv"] | ||||
| size = Vector2(64, 64) | ||||
|  | ||||
| [node name="Player" type="CharacterBody2D"] | ||||
| script = ExtResource("1_mbyc2") | ||||
|  | ||||
| [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] | ||||
| frames = SubResource("SpriteFrames_mqbrd") | ||||
|  | ||||
| [node name="CollisionShape2D" type="CollisionShape2D" parent="."] | ||||
| shape = SubResource("RectangleShape2D_ng5bv") | ||||
		Reference in New Issue
	
	Block a user