| | | 1 | | using Godot; |
| | | 2 | | |
| | | 3 | | |
| | | 4 | | namespace Safarimacik.Model; |
| | | 5 | | |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Base class for all placeable objects. |
| | | 9 | | /// </summary> |
| | | 10 | | public interface Purchasable { |
| | | 11 | | public int Price { get; } |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Static class for purchasing a purchasable object. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class PurchasableFactory { |
| | 1 | 18 | | private static readonly Func<Vector2, Purchasable>[] registry = [ |
| | 1 | 19 | | (position) => new Sheep(position), |
| | 1 | 20 | | (position) => new Cow(position), |
| | 1 | 21 | | (position) => new Wolf(position), |
| | 1 | 22 | | (position) => new Fox(position), |
| | 1 | 23 | | (position) => Weed.Instance, // Assuming Weed.Instance doesn't require a position |
| | 1 | 24 | | (position) => new Bush(), |
| | 1 | 25 | | (position) => new Tree(), |
| | 1 | 26 | | (position) => new Road(), |
| | 1 | 27 | | (position) => new Water(), |
| | 1 | 28 | | (position) => new Jeep([], position, 0), // Handle null position for Jeep |
| | 1 | 29 | | (position) => new Ranger(position) |
| | 1 | 30 | | ]; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Purchases an object. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="purchaseId">Id of the object to be purchased</param> |
| | | 36 | | /// <param name="funds">Funds for the purchase</param> |
| | | 37 | | /// <param name="position">Position with which the object is constructed</param> |
| | | 38 | | /// <returns>Null if the funds are insufficient, the purchased object otherwise</returns> |
| | 1 | 39 | | public static Purchasable? Purchase(int purchaseId, int funds, Vector2 position) { |
| | 1 | 40 | | Purchasable candidate = registry[purchaseId](position); |
| | 1 | 41 | | if (funds >= candidate.Price) { |
| | 1 | 42 | | return candidate; |
| | | 43 | | } |
| | 0 | 44 | | return null; |
| | 1 | 45 | | } |
| | | 46 | | } |