You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.0 KiB

using Godot;
using Godot.NativeInterop;
using System;
using System.Collections.Generic;
using System.Numerics;
public partial class SlotMachine : Sprite2D
{
[Export] public Wheel[] Wheels;
int _spins;
int _points;
bool _isSpinning = false;
public int Spins
{
get
{
return _spins;
}
set
{
_spins = value;
GetNode<SpinsUI>("%Spins").Text = _spins + " spins";
}
}
public int Points
{
get
{
return _points;
}
set
{
_points = value;
GetNode<Label>("%Points").Text = _points.ToString();
}
}
public override void _Ready()
{
Spins = 7;
GetOptionPicker().Show(false);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (_isSpinning)
{
bool stillSpinning = false;
foreach (Wheel wheel in Wheels)
{
if (wheel.Spinning)
stillSpinning = true;
}
if (stillSpinning == false)
{
Score();
_isSpinning = false;
}
}
}
private void Score()
{
List<SlotSymbol> slots = new List<SlotSymbol>();
int points = 0;
bool x2 = false;
foreach (Wheel wheel in Wheels)
{
SlotSymbol newSlotSymbol = wheel.GetActiveSlotSymbol();
int matches = 0;
foreach (SlotSymbol otherSlot in slots)
{
if (otherSlot.SlotType == newSlotSymbol.SlotType && newSlotSymbol.SlotType != SlotSymbol.Type.SPIN && newSlotSymbol.SlotType != SlotSymbol.Type.X2)
{
matches++;
}
}
if (newSlotSymbol.SlotType == SlotSymbol.Type.SEVEN)
{
points += 7;
}
else if (newSlotSymbol.SlotType == SlotSymbol.Type.SPIN)
{
Spins++;
}
else if (newSlotSymbol.SlotType == SlotSymbol.Type.X2)
{
x2 = true;
}
slots.Add(newSlotSymbol);
if (matches == 1) {
points += 25;
}
else if (matches == 2)
{
points += 100;
}
}
if (x2)
{
points *= 2;
}
GetNode<Label>("%Readout").Text = points.ToString();
Points += points;
GetOptionPicker().Reset();
GetOptionPicker().Show(true);
}
OptionPicker GetOptionPicker()
{
return GetNode<OptionPicker>("%OptionPicker");
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Enter || keyEvent.Keycode == Key.KpEnter)
{
Spin();
}
if (_isSpinning == false)
{
if (keyEvent.Keycode == Key.Key1)
{
Wheels[0].ReplaceCurrentOption(GetOptionPicker().Select(0));
}
if (keyEvent.Keycode == Key.Key2)
{
Wheels[1].ReplaceCurrentOption(GetOptionPicker().Select(1));
}
if (keyEvent.Keycode == Key.Key3)
{
Wheels[2].ReplaceCurrentOption(GetOptionPicker().Select(2));
}
}
}
}
private void Spin()
{
if (Spins <= 0 || _isSpinning)
return;
GetOptionPicker().Show(false);
GetNode<Label>("%Readout").Text = "";
_isSpinning = true;
Spins--;
float extraLength = 0f;
foreach (Wheel wheel in Wheels)
{
wheel.Spin(extraLength);
extraLength += 1f;
}
}
}