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.

219 lines
5.2 KiB

using Godot;
2 days ago
using Godot.NativeInterop;
using System;
2 days ago
using System.Collections.Generic;
2 days ago
using System.Numerics;
public partial class SlotMachine : Sprite2D
{
[Export] public Wheel[] Wheels;
2 days ago
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 = 9;
2 days ago
GetOptionPicker().Show(false);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
2 days ago
if (_isSpinning)
{
bool stillSpinning = false;
foreach (Wheel wheel in Wheels)
{
if (wheel.Spinning)
stillSpinning = true;
}
if (stillSpinning == false)
{
Score();
_isSpinning = false;
}
}
}
2 days ago
private void Score()
{
2 days ago
List<SlotSymbol> slots = new List<SlotSymbol>();
int points = 0;
2 days ago
bool x2 = false;
2 days ago
foreach (Wheel wheel in Wheels)
{
2 days ago
SlotSymbol newSlotSymbol = wheel.GetActiveSlotSymbol();
int matches = 0;
2 days ago
2 days ago
foreach (SlotSymbol otherSlot in slots)
{
2 days ago
if (otherSlot.SlotType == newSlotSymbol.SlotType && newSlotSymbol.SlotType != SlotSymbol.Type.SPIN && newSlotSymbol.SlotType != SlotSymbol.Type.X2)
2 days ago
{
matches++;
}
2 days ago
}
2 days ago
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;
}
else if (newSlotSymbol.SlotType == SlotSymbol.Type.CHERRY)
{
if (matches == 2)
{
points += 15;
}
else if (matches == 1)
{
points += 3;
}
else
{
points += 2;
}
}
else if (newSlotSymbol.SlotType == SlotSymbol.Type.BANANA)
{
if (matches == 2)
{
points += 20;
}
else if (matches == 1)
{
points += 10;
}
2 days ago
}
else if (newSlotSymbol.SlotType == SlotSymbol.Type.BELL)
2 days ago
{
if (matches == 2)
{
points += 75;
}
else if (matches == 1)
{
points += 25;
}
2 days ago
}
else if (newSlotSymbol.SlotType == SlotSymbol.Type.BAR)
{
if (matches == 2)
{
points += 150;
}
}
slots.Add(newSlotSymbol);
2 days ago
}
if (x2)
{
points *= 2;
2 days ago
}
GetNode<Label>("%Readout").Text = "+" + points.ToString();
2 days ago
Points += points;
2 days ago
GetOptionPicker().Reset();
GetOptionPicker().Show(true);
HideReadout();
2 days ago
}
async void HideReadout()
{
await ToSignal(GetTree().CreateTimer(1.5f), SceneTreeTimer.SignalName.Timeout);
GetNode<Label>("%Readout").Text = "";
}
2 days ago
OptionPicker GetOptionPicker()
{
return GetNode<OptionPicker>("%OptionPicker");
2 days ago
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Enter || keyEvent.Keycode == Key.KpEnter || keyEvent.Keycode == Key.Space)
2 days ago
{
Spin();
}
2 days ago
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));
}
}
2 days ago
}
}
private void Spin()
{
if (Spins <= 0 || _isSpinning)
return;
2 days ago
GetOptionPicker().Show(false);
2 days ago
GetNode<Label>("%Readout").Text = "";
_isSpinning = true;
Spins--;
float extraLength = 0f;
foreach (Wheel wheel in Wheels)
{
wheel.Spin(extraLength);
extraLength += 1f;
}
}
}