HeavenStudio/Assets/Scripts/Games/Spaceball/SpaceballPlayer.cs

89 lines
2.2 KiB
C#
Raw Normal View History

2023-02-20 05:48:50 +00:00
using System;
using System.Collections.Generic;
2023-02-20 05:48:50 +00:00
using UnityEngine;
2022-03-14 14:21:05 +00:00
using HeavenStudio.Util;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Games.Scripts_Spaceball
{
public class SpaceballPlayer : MonoBehaviour
{
2023-02-20 05:48:50 +00:00
private Animator _anim;
private int _currentCostume;
2021-12-26 07:25:17 +00:00
public SpriteRenderer PlayerSprite;
2023-02-20 05:48:50 +00:00
public SpriteRenderer Hat;
public HatSprite HatSprites1 = new HatSprite();
public HatSprite HatSprites2 = new HatSprite();
2021-12-26 07:25:17 +00:00
2023-02-20 05:48:50 +00:00
[Serializable]
public struct HatSprite
2021-12-26 07:25:17 +00:00
{
2023-02-20 05:48:50 +00:00
public List<Vector2> Offsets;
public List<Sprite> Sprites;
2021-12-26 07:25:17 +00:00
}
public static SpaceballPlayer instance { get; set; }
private void Awake()
{
instance = this;
2023-02-20 05:48:50 +00:00
_anim = GetComponent<Animator>();
}
private void Update()
{
if (PlayerInput.Pressed())
{
Swing(null);
}
}
2023-02-20 05:48:50 +00:00
public void SetCostume(Material mat, int costumeIndex)
2021-12-26 07:25:17 +00:00
{
2023-02-20 05:48:50 +00:00
PlayerSprite.material = mat;
_currentCostume = costumeIndex;
_anim.Play("Idle", 0, 0);
2021-12-26 07:25:17 +00:00
}
public void Swing(SpaceballBall b)
{
if (b == null)
{
2022-01-21 01:24:30 +00:00
Jukebox.PlayOneShotGame("spaceball/swing");
}
else
{
2021-12-26 07:25:17 +00:00
}
2023-02-20 05:48:50 +00:00
_anim.Play("Swing", 0, 0);
}
2023-02-20 05:48:50 +00:00
public void SetHatFrame(int frame)
2021-12-26 07:25:17 +00:00
{
2023-02-20 05:48:50 +00:00
// Unity can't serialize lists inside lists in this version, so that's annoying.
var sprites = new HatSprite();
switch (_currentCostume)
{
case 0:
return;
case 1:
sprites = HatSprites1;
break;
case 2:
sprites = HatSprites2;
break;
}
if (sprites.Sprites.Count - 1 < frame)
frame = 0;
Hat.sprite = sprites.Sprites[frame];
var offset = Vector2.zero;
if (sprites.Offsets.Count - 1 >= frame)
offset = sprites.Offsets[frame];
Hat.transform.localPosition = offset;
2021-12-26 07:25:17 +00:00
}
}
}