create goomba behavior

This commit is contained in:
Chris Cromer 2022-11-21 00:42:55 -03:00
parent a567b0c3a3
commit 6ffb2a58d0
4 changed files with 83 additions and 3 deletions

19
scenes/enemies/Enemy.cs Normal file
View File

@ -0,0 +1,19 @@
using Godot;
using System;
public class Enemy : KinematicBody2D
{
[Export]
private Vector2 _startPosition;
public Vector2 StartPosition { get { return _startPosition; } }
public override void _Ready()
{
_startPosition = Position;
}
public virtual void Reset()
{
Position = _startPosition;
}
}

View File

@ -0,0 +1,31 @@
using Godot;
using System;
public class EnemyWalking : Enemy
{
[Export]
private int _gravity = (int) ProjectSettings.GetSetting("physics/2d/default_gravity");
private Vector2 _velocity;
public override void _Ready()
{
base._Ready();
_velocity = new Vector2();
}
public override void _PhysicsProcess(float delta)
{
_velocity.y += (int) _gravity * delta * 5;
_velocity.x = 25 * -1;
_velocity = MoveAndSlide(_velocity, Vector2.Up);
Mathf.Round(Position.x);
Mathf.Round(Position.y);
}
public override void Reset()
{
base.Reset();
_velocity = new Vector2();
}
}

View File

@ -0,0 +1,19 @@
using Godot;
using System;
public class Goomba : EnemyWalking
{
private AnimatedSprite _sprite;
public override void _Ready()
{
base._Ready();
_sprite = GetNode<AnimatedSprite>("AnimatedSprite");
_sprite.Play("walk");
}
public void OnVisibilityNotifier2DScreenExited()
{
Reset();
}
}

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=8 format=2]
[ext_resource path="res://assets/goomba/walking.png" type="Texture" id=1]
[ext_resource path="res://scenes/enemies/goomba/Goomba.cs" type="Script" id=2]
[sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 )
@ -23,10 +24,11 @@ animations = [ {
} ]
[sub_resource type="RectangleShape2D" id=5]
extents = Vector2( 6.5, 7 )
extents = Vector2( 6.67969, 7 )
[node name="Goomba" type="KinematicBody2D" groups=["Enemy", "Stompable"]]
collision_layer = 4
script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 4 )
@ -34,5 +36,14 @@ animation = "walk"
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
visible = false
position = Vector2( 0.5, 2 )
position = Vector2( 0.320312, 2 )
shape = SubResource( 5 )
[node name="VisibilityEnabler2D" type="VisibilityEnabler2D" parent="."]
visible = false
position = Vector2( 1, 0 )
rect = Rect2( -9, -9, 18, 18 )
process_parent = true
physics_process_parent = true
[connection signal="screen_exited" from="VisibilityEnabler2D" to="." method="OnVisibilityNotifier2DScreenExited"]