ci-platforms / windows amd64 smoke (push) Skipped
ci / release (push) Successful in 8m58s
ci-platforms / darwin amd64 smoke (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
ci / checks (push) Successful in 4m46s
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 13m28s
199 lines
4.1 KiB
Plaintext
199 lines
4.1 KiB
Plaintext
package player
|
|
|
|
import (
|
|
"aego/anim"
|
|
"aego/audio"
|
|
"aego/camera"
|
|
"aego/input"
|
|
"aego/log"
|
|
"aego/math"
|
|
"aego/save"
|
|
"aego/settings"
|
|
"mygame/enemy"
|
|
)
|
|
|
|
script PlayerController {
|
|
@export
|
|
var EnemyA NodeRef
|
|
|
|
@export
|
|
var EnemyB NodeRef
|
|
|
|
@export(min=0)
|
|
var LandShake float = 9
|
|
|
|
@export(gizmo="vector")
|
|
var Bounce Vec2 = Vec2{0, -9}
|
|
|
|
@export
|
|
var JumpSound AssetRef<AudioEvent>
|
|
|
|
@export
|
|
var HitSound AssetRef<AudioEvent>
|
|
|
|
@export
|
|
var Theme AssetRef<Clip>
|
|
|
|
@export(tip="Hitbox that the Hit frame of the attack clip pulses")
|
|
var Slash NodeRef
|
|
|
|
@export(tip="Landing dust emitter")
|
|
var Dust NodeRef
|
|
|
|
@export(tip="Sparks for the sword swing")
|
|
var Sparks NodeRef
|
|
|
|
@export(min=0)
|
|
var Reach float = 0.6
|
|
|
|
@persist
|
|
var kills int
|
|
var facing float = 1
|
|
var hit int
|
|
var motionPhase float
|
|
var stompBouncing bool
|
|
|
|
func Ready() {
|
|
watch(EnemyA)
|
|
watch(EnemyB)
|
|
m := node.Get<Motion2D>()
|
|
m.Jumped.Connect(OnJumped)
|
|
m.Landed.Connect(OnLanded)
|
|
m.Climbed.Connect(OnClimbed)
|
|
node.Get<Hitbox2D>().Hit.Connect(OnStomp)
|
|
node.Get<Health>().Damaged.Connect(OnDamaged)
|
|
hit = anim.EventID("Hit")
|
|
if Slash != nil && Slash.IsAlive() {
|
|
Slash.SetPosition(Vec2{facing * Reach, 0})
|
|
}
|
|
audio.Music(Theme, 1.5)
|
|
}
|
|
|
|
func watch(n NodeRef) {
|
|
if n != nil && n.IsAlive() {
|
|
n.GetScript<enemy.EnemyAI>().Died.Connect(OnEnemyDied)
|
|
}
|
|
}
|
|
|
|
func OnEnemyDied() {
|
|
kills++
|
|
save.SetGlobalInt("kills", save.GlobalInt("kills", 0) + 1)
|
|
}
|
|
|
|
func OnStomp(target NodeRef, damage int) {
|
|
m := node.Get<Motion2D>()
|
|
if !stompBouncing && m.Velocity.Y >= 0 {
|
|
stompBouncing = true
|
|
m.AddVelocity(Vec2{0, Bounce.Y - m.Velocity.Y})
|
|
}
|
|
}
|
|
|
|
func OnJumped() {
|
|
audio.PlayEvent(JumpSound)
|
|
if Dust != nil && Dust.IsAlive() {
|
|
Dust.Get<ParticleSystem2D>().Burst(5)
|
|
}
|
|
}
|
|
|
|
func OnClimbed() {
|
|
if Dust != nil && Dust.IsAlive() {
|
|
Dust.Get<ParticleSystem2D>().Burst(6)
|
|
}
|
|
}
|
|
|
|
func OnLanded(speed float) {
|
|
if Dust != nil && Dust.IsAlive() {
|
|
Dust.Get<ParticleSystem2D>().Burst(8)
|
|
}
|
|
if speed > LandShake {
|
|
camera.Shake(0.35)
|
|
}
|
|
}
|
|
|
|
func OnDamaged(amount int, source NodeRef) {
|
|
audio.PlayEvent(HitSound)
|
|
node.Get<Animator>().SetTrigger("Hurt")
|
|
}
|
|
|
|
func OnAnimEvent(id int, payload float) {
|
|
if id == hit && Slash != nil {
|
|
Slash.SetPosition(Vec2{facing * Reach, 0})
|
|
Slash.Get<Hitbox2D>().Pulse()
|
|
camera.Shake(0.08)
|
|
if Sparks != nil && Sparks.IsAlive() {
|
|
Sparks.Get<ParticleSystem2D>().Burst(8)
|
|
}
|
|
}
|
|
}
|
|
|
|
func FixedUpdate(dt float) {
|
|
m := node.Get<Motion2D>()
|
|
if m.Velocity.Y < 0 {
|
|
stompBouncing = false
|
|
}
|
|
x := input.Axis("Move")
|
|
m.Move(x)
|
|
if input.Pressed("Jump") {
|
|
m.Jump()
|
|
}
|
|
if input.Released("Jump") {
|
|
m.JumpReleased()
|
|
}
|
|
if x != 0 {
|
|
facing = math.Sign(x)
|
|
node.Get<SpriteRenderer>().FlipX = x < 0
|
|
if Slash != nil {
|
|
Slash.SetPosition(Vec2{facing * Reach, 0})
|
|
Slash.Get<SpriteRenderer>().FlipX = x < 0
|
|
}
|
|
}
|
|
node.Get<Hitbox2D>().Active = !m.Grounded && m.Velocity.Y > 0
|
|
a := node.Get<Animator>()
|
|
a.SetFloat("Speed", math.Abs(m.Velocity.X))
|
|
a.SetBool("Grounded", m.Grounded)
|
|
a.SetFloat("VerticalSpeed", m.Velocity.Y)
|
|
a.SetBool("Climbing", m.Climbing)
|
|
if input.Pressed("Attack") {
|
|
a.SetTrigger("Attack")
|
|
}
|
|
if Slash != nil && a.State != "Hit" {
|
|
Slash.Get<SpriteRenderer>().Tint = Color{1, 1, 1, 0}
|
|
}
|
|
}
|
|
|
|
func Update(dt float) {
|
|
motionPhase += dt
|
|
m := node.Get<Motion2D>()
|
|
if node.Get<Animator>().State != "Hit" && node.Get<Animator>().State != "Climb" && !m.Climbing {
|
|
if !m.Grounded {
|
|
if m.Velocity.Y < 0 {
|
|
node.Get<SpriteRenderer>().Size = Vec2{0.92, 1.1}
|
|
} else {
|
|
node.Get<SpriteRenderer>().Size = Vec2{1.08, 0.94}
|
|
}
|
|
} else {
|
|
pulse := math.Sin(motionPhase * 4) * 0.025
|
|
if math.Abs(m.Velocity.X) > 0.2 {
|
|
pulse = math.Sin(motionPhase * 20) * 0.045
|
|
}
|
|
node.Get<SpriteRenderer>().Size = Vec2{1 - pulse, 1 + pulse}
|
|
}
|
|
}
|
|
if input.Pressed("QuickSave") {
|
|
save.Save("quick")
|
|
}
|
|
if input.Pressed("QuickLoad") && save.Exists("quick") {
|
|
save.Load("quick")
|
|
}
|
|
if input.Pressed("Pause") {
|
|
settings.OpenView()
|
|
}
|
|
}
|
|
|
|
func OnSettingChanged(id string) {
|
|
if id == "audio.bus.music.volume" {
|
|
log.Info("music volume " + string(settings.GetFloat(id)))
|
|
}
|
|
}
|
|
}
|