Hi All,
I hope you're all well. I'm new to Godot, well I am new to game dev too. I can't code first up, I have followed a tutorial (UmaiPixel 2D platformer series) on line which for me was a perfect starting point. I did learn a lot, I understand why and what he is coding but going solo is a differnt story, I am watching many online tutorials and slowly, it's sinking in. I have the player able to fire and kill enemies but because I'm not sure on the coding side I can't reverse engineer what I have learned in order to have enemies detect and fire back at the player (My game is a platformer)
I understand that I need a enemy fire animation, an area 2D where the enemy fires from, a collision detector on the enemy for when the player enters that area, I only know this because that's similar to what I did for the player, without the detector. I just can't code though. I have made a backup folder so I can test my game incase I broke it, which I knew I would. Any help with the code side of things on this matter would be very very much appreciated. Below is the code for the player and the enemy, sorry for the mess.
**Player**
extends KinematicBody2D
const SPEED = 60
const GRAVITY = 5
const JUMP_POWER = -150
const FLOOR = Vector2(0, -1)
const FIREBALL = preload("res://Fireball02.tscn")
const LASERBLUE = preload("res://LaserBlue.tscn")
var velocity = Vector2()
var on_ground = false
var is_attacking = false
var is_dead = false
var fireball_power = 1
func physicsprocess(delta):
if is_dead == false:
if Input.is_action_pressed("ui_right"):
if is_attacking == false or is_on_floor() == false:
velocity.x = SPEED
if is_attacking == false:
$AnimatedSprite.play("run")
$AnimatedSprite.flip_h = false
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1
elif Input.is_action_pressed("ui_left"):
if is_attacking == false or is_on_floor() == false:
velocity.x = -SPEED
if is_attacking == false:
$AnimatedSprite.play("run")
$AnimatedSprite.flip_h = true
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1
else:
velocity.x = 0
if on_ground == true && is_attacking == false:
$AnimatedSprite.play("idle")
if Input.is_action_pressed("ui_up"):
if is_attacking == false:
if on_ground == true:
velocity.y = JUMP_POWER
on_ground = false
if Input.is_action_just_pressed("ui_focus_next") && is_attacking == false:
$LaserSound.play()
if is_on_floor():
velocity.x = 0
is_attacking = true
$AnimatedSprite.play("attack")
var fireball = null
if fireball_power == 1:
fireball = FIREBALL.instance()
elif fireball_power == 2:
fireball = LASERBLUE.instance()
if sign($Position2D.position.x) == 1:
fireball.set_fireball_direction(1)
else:
fireball.set_fireball_direction(-1)
get_parent().add_child(fireball)
fireball.position = $Position2D.global_position
velocity.y += GRAVITY
if is_on_floor():
if on_ground == false:
is_attacking = false
on_ground = true
else:
if is_attacking == false:
on_ground = false
if velocity.y < 0:
$AnimatedSprite.play("jump")
else:
$AnimatedSprite.play("fall")
velocity = move_and_slide(velocity, FLOOR)
if get_slide_count() > 0:
for i in range(get_slide_count()):
if "Enemy" in get_slide_collision(i).collider.name:
dead()
func dead():
is_dead = true
velocity = Vector2(0, 0)
$AnimatedSprite.play("dead")
$CollisionShape2D.disabled = true
$Timer.start()
func onAnimatedSpriteanimationfinished():
is_attacking = false
func onTimertimeout():
gettree().change_scene("res://TitleScreen.tscn")
func gunpowerup():
fireball_power = 2
Enemy
extends KinematicBody2D
const GRAVITY = 10
const FLOOR = Vector2(0, -1)
var velocity = Vector2()
var direction = 1
var death = preload("res://Explosion.tscn")
var collision = moveandslide(velocity *delta)
var is_dead = false
export(int) var speed = 30
export(int) var hp = 1
export(Vector2) var size = Vector2(1, 1)
func _ready():
scale = size
pass
func dead(damage):
hp = hp - damage
if hp <= 0:
isdead = true
velocity = Vector2(0, 0)
$AnimatedSprite.play("dead")
#$explosion.play()
# following line does not work in v3.1
# $CollisionShape2D.disabled = true
$CollisionShape2D.setdeferred("disabled", true)
$Timer.start()
if scale > Vector2(1, 1):
getparent().getnode("ScreenShake").screen_shake(1, 10, 100)
func physicsprocess(delta):
if is_dead == false:
velocity.x = speed * direction
if direction == 1:
$AnimatedSprite. flip_h = false
else:
$AnimatedSprite. flip_h = true
$AnimatedSprite.play("walk")
velocity.y += GRAVITY
velocity = move_and_slide(velocity, FLOOR)
if is_on_wall():
direction = direction * -1
$RayCast2D.position.x *= -1
if $RayCast2D.is_colliding() == false:
direction = direction * -1
$RayCast2D.position.x *= -1
if get_slide_count() > 0:
for i in range (get_slide_count()):
if "Player" in get_slide_collision(i).collider.name:
get_slide_collision(i).collider.dead()
func onTimertimeout():
queuefree()