-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharacterBody2D.gd
51 lines (40 loc) · 1.37 KB
/
CharacterBody2D.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
extends CharacterBody2D
# Stałe prędkości i siły skoku
const SPEED = 300
const JUMP_FORCE = -250
# Grawitacja
const GRAVITY = 100
# Własna zmienna do przechowywania prędkości
var custom_velocity = Vector2(0, 0)
# Limit kamery
var camera_limits = Rect2()
func _ready():
# Pobierz obszar widoc zny kamery jako limit ruchu
camera_limits = $MainCamera.get_viewport_rect()
func _physics_process(delta):
# Sterowanie postacią za pomocą klawiszy
custom_velocity.x = 0
if Input.is_action_pressed("ui_right"):
custom_velocity.x += SPEED
if Input.is_action_pressed("ui_left"):
custom_velocity.x -= SPEED
if is_on_floor() and Input.is_action_pressed("ui_up"):
custom_velocity.y = JUMP_FORCE
# Dodaj grawitację
custom_velocity.y += GRAVITY * delta
print(custom_velocity)
# Porusz postacią
velocity = custom_velocity
move_and_slide()
custom_velocity = velocity
# Ograniczenie kamery
var position = global_position
if position.x < camera_limits.position.x:
position.x = camera_limits.position.x
elif position.x > camera_limits.position.x + camera_limits.size.x:
position.x = camera_limits.position.x + camera_limits.size.x
# if position.y < camera_limits.position.y:
# position.y = camera_limits.position.y
# elif position.y > camera_limits.position.y + camera_limits.size.y:
# position.y = camera_limits.position.y + camera_limits.size.y
global_position = position