76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class Blorbo : CharacterBody2D
|
|
{
|
|
public const float TerminalVelocity = 500.0f; // Max falling speed
|
|
public const float Speed = 60.0f;
|
|
public const float JumpVelocity = -160.0f;
|
|
public const float Acceleration = 400.0f; // How much we speed up/second
|
|
public const float Decelleration = 400.0f; // Slow down/second
|
|
public const float AirFrictionMultiplier = 0.2f; // How much less control and speedup we have in the air
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
Vector2 velocity = Velocity;
|
|
|
|
// Add the gravity.
|
|
if (!IsOnFloor())
|
|
{
|
|
velocity += GetGravity() * (float)delta;
|
|
velocity.Y = Math.Min(TerminalVelocity, velocity.Y);
|
|
}
|
|
|
|
// Handle Jump.
|
|
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
|
|
{
|
|
velocity.Y = JumpVelocity;
|
|
}
|
|
|
|
// Get the input direction and handle the movement/deceleration.
|
|
// As good practice, you should replace UI actions with custom gameplay actions.
|
|
Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
|
|
float friction_multiplier = 1.0f * getFloorFriction();
|
|
if (!IsOnFloor()){
|
|
friction_multiplier *= AirFrictionMultiplier;
|
|
}
|
|
if (direction != Vector2.Zero)
|
|
{
|
|
velocity.X += direction.X * Acceleration * (float)delta * friction_multiplier;
|
|
velocity.X = Math.Min(Speed, velocity.X);
|
|
}
|
|
else
|
|
{
|
|
|
|
velocity.X = Mathf.MoveToward(Velocity.X, 0, Decelleration * (float)delta * friction_multiplier);
|
|
}
|
|
|
|
Velocity = velocity;
|
|
MoveAndSlide();
|
|
}
|
|
|
|
private float getFloorFriction() {
|
|
Marker2D floorCheck = GetNode<Marker2D>("FloorCheck");
|
|
Vector2 checkedPosition = floorCheck.GlobalPosition;
|
|
GD.Print(checkedPosition);
|
|
|
|
for (int i = 0; i < GetSlideCollisionCount(); i++)
|
|
{
|
|
var collision = GetSlideCollision(i);
|
|
var collider = collision.GetCollider();
|
|
if (collider is TileMapLayer tileMap)
|
|
{
|
|
TileData data = tileMap.GetCellTileData(
|
|
tileMap.LocalToMap(
|
|
tileMap.ToLocal(checkedPosition)));
|
|
GD.Print(collider.ToString());
|
|
if (data != null)
|
|
{
|
|
return (float)data.GetCustomData("frictionMul");
|
|
}
|
|
}
|
|
}
|
|
return 1.0f;
|
|
}
|
|
}
|