Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | BryanGieger |
Hello everyone, I’m starting with godot, I already did the tutorial for my first game, now I wanted to make my own version with that knowledge.
I wanted to do a Space Shooter, everything is fine until I wanted to put the shooting mechanics, I started to investigate and came across another tutorial from the documentation:
Link to documentation article
I did as it says what it says adapting it and making it not take the mouse as a reference.
The problem is Mono gives me the following errors:
- Main.cs (125,34): error CS0029: Cannot implicitly convert type ‘Godot.Vector2’ to ‘float’
- Main.cs (127,23): error CS0122: ‘Bullet.Velocity’ is not accessible due to its protection level
- Main.cs (127,48): error CS0122: ‘Bullet.Velocity’ is not accessible due to its protection level
Here are the scripts:
Bullet Script
public class Bullet : Area2D
{
Vector2 Velocity = new Vector2();
public override void _PhysicsProcess(float delta)
{
Position -= Velocity * delta;
}
public void _ViewportExcited()
{
QueueFree();
}
}
Player Script
public class Player : Area2D
{
[Signal]
public delegate void Hit();
[Export]
public int velocidad;
[Signal]
delegate void Shoot(PackedScene bullet, Vector2 direction, Vector2 location);
private PackedScene _bullet = GD.Load<PackedScene>("res://Scenes/Bullet.tscn");
private Vector2 _tamañoPantalla;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_tamañoPantalla = GetViewport().Size;
//Oculta al jugador al inicio del juego
Hide();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
var aceleracion = new Vector2(); //Vector 2D de movimiento del jugador
var fireAnimation = GetNode<AnimatedSprite>("AnimatedSprite");
if (Input.IsActionPressed("ui_right"))
{
aceleracion.x += 1;
fireAnimation.Animation = "FireIzq";
}
if (Input.IsActionPressed("ui_left"))
{
aceleracion.x -= 1;
fireAnimation.Animation = "FireDer";
}
if (aceleracion.Length() > 0)
{
aceleracion = aceleracion.Normalized() * velocidad;
}
Position += aceleracion * delta;
Position = new Vector2
(
x: Mathf.Clamp(Position.x, 22, _tamañoPantalla.x - 22),
y: Mathf.Clamp(Position.y, 0, _tamañoPantalla.y)
);
if (aceleracion.Length() == 0)
{
fireAnimation.Animation = "FireDown";
}
if (Input.IsActionPressed("ui_shoot"))
{
EmitSignal(nameof(Shoot), _bullet, Rotation, Position);
}
}
private void _on_Player_body_entered(object body)
{
Hide();
EmitSignal("Hit");
GetNode<CollisionPolygon2D>("CollisionPolygon2D").SetDeferred("disabled", true);
}
public void Empezar(Vector2 pos)
{
Position = pos;
Show();
GetNode<CollisionPolygon2D>("CollisionPolygon2D").Disabled = false;
}
}
Main Script
public class Main : Node
{
[Export]
public PackedScene[] asteroides;
[Export]
public PackedScene Mob;
private int _distancia;
private int _score;
private Random _random = new Random();
public override void _Ready()
{
NewGame();
}
private float RandRange(float min, float max)
{
return (float)_random.NextDouble() * (max - min) + min;
}
private void GameOver()
{
GetNode<Timer>("AsteroidesTimer").Stop();
GetNode<Timer>("MobTimer").Stop();
GetNode<Timer>("DistanceTimer").Stop();
}
private void NewGame()
{
_distancia = 0;
var player = GetNode<Player>("Player");
var startPosition = GetNode<Position2D>("PosicionInicial");
player.Empezar(startPosition.Position);
GetNode<Timer>("StartTimer").Start();
}
private void _on_StartTimer_timeout()
{
GetNode<Timer>("DistanceTimer").Start();
GetNode<Timer>("MobTimer").Start();
GetNode<Timer>("AsteroidesTimer").Start();
}
private void _on_DistanceTimer_timeout()
{
_distancia++;
}
private void _on_MobTimer_timeout()
{
//Selecciona una poscion random en el Path2d
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawn");
mobSpawnLocation.Offset = _random.Next();
//Crea una instancia del mob y la agrega a la escena
var instanciaMob = (RigidBody2D)Mob.Instance();
AddChild(instanciaMob);
//Pone una direccion perpendicular a la direccion del path.
float direccion = mobSpawnLocation.Rotation + Mathf.Pi / 2;
//Pone al mob en una posicion random
instanciaMob.Position = mobSpawnLocation.Position;
//Agrega una direccion random
direccion += RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
instanciaMob.Rotation = direccion;
//Selecciona una velocidad
instanciaMob.LinearVelocity = new Vector2(RandRange(150f, 250f), 0).Rotated(direccion);
}
private void _on_AsteroidesTimer_timeout()
{
//Selecciona una poscion random en el Path2d
var asteroideSpawnLocation = GetNode<PathFollow2D>("AsteroidesPath/AsteroidesSpawn");
asteroideSpawnLocation.Offset = _random.Next();
//Crea una instancia del mob y la agrega a la escena
var instanciaAsteroide = (RigidBody2D)asteroides[_random.Next(0,7)].Instance();
AddChild(instanciaAsteroide);
//Pone al mob en una posicion random
instanciaAsteroide.Position = asteroideSpawnLocation.Position;
}
public void _on_Player_Shoot(PackedScene bullet, Vector2 direction, Vector2 location)
{
//here mono marks the error
var instanciaBala = (Bullet)bullet.Instance();
GetParent().AddChild(instanciaBala);
instanciaBala.Rotation = direction;
instanciaBala.Position = location;
instanciaBala.Velocity = instanciaBala.Velocity.Rotated(direction);
}
}