I've got a Gameboard containing a lot of dices.
Every can be tilted to either direction (limited by the boundaries of the Gameboard and other dices in their way) by dragging the mouse on top of it in on of the four directions.
So the dices themself handle the input but before executing a tilt I have to check whether the move is allowed on the gameboard.
Therefore I'd give every dice a reference to the gameboard but is there a better way to get this two-way behaviour?
Instead of handling this logic inside of the dice objects with a reference to the Gameboard, I'd recommend looking at doing the opposite and instead handling this in an an abstracted system (creating a 3rd element that bridges these two components together and handles the required logic), checking whether each move is valid or not.
To do this you'd need to change the mouse input so that it is also handled in an abstracted system rather than on each Dice, so when you touch a Dice object this system is able to check which object was touched, before then running the code to check if the move is valid on that object. This would most likely involve you storing a List/Array/Dictionary of all the Dice objects, so that you're able to check and see if the touched object is contained within and therefore valid. You can then get any properties you need from the Dice object by using touchedObject.GetComonent<Dice>(); (or you can store all these properties separately from the actual MonoBehaviour and instead look them up).
With this method you can check against the Dice object and the gameboard to see if the next move is valid, without having each object being dependant on the other (which would cause circular dependencies). This also gives the added advantage of giving you more flexibility to change or remove this code going forwards.
This is one of many different approaches you could take, another would be to have your gameboard store a reference of each Dice object and have the Dice object fire an event when interact with (that the gameboard is listening for), so the gameboard code is then able run the check - though from experience I'd go with the former approach and look at decoupling as much as possible.
Related
EDIT: I found a solution that works without needing to overhaul much of what I've done... and made myself feel like a fool, because it was at the top of my notes from when I started this project a week ago.
A dictionary containing names and IDs of items will exist, the game will add the ID of any given item to the mixing pot list, sort them in numerical order, and compare the result to a recipe dictionary.
I'll leave the original question here in case anyone else happens to have my weirdly specific problem.
The basic problem I aim to solve is a system in which the player can add ingredients to a mixing pot, click a button, and the game will read the list of ingredients and return a potion (or any other item, but I'm speaking on potions for simplicity's sake). The recipes for potions can get somewhat specific, and potions may need multiple of specific ingredients. The player might also add ingredients in an arbitrary order, and I want to account for that.
My original plan, when I started this project, was for each item to be loaded from a JSON or something, where they'd have a formID based on their list position. This formID would be taken for all the items thrown in the mixing pot, sorted, then checked against a dictionary of all craftable items to find what potion was made.
The problem I've run into, however, is that the detail and scale of my item system has grown to the point where each item is its own programmed object interfacing with a list of traits, each of which is their own programmed object with an interface for determining their parent items' function. For a clearer explanation, the structure I've gone with derives from this post: https://gamedev.stackexchange.com/questions/147873/creating-a-robust-item-system
Constructing things in this way allows for me to have an immense amount of flexibility in what any given item can do, what can be done with it, and what it can interact with. However, I can't be certain of the exact number of components or properties any given item should have, which makes building a JSON a bit complicated. I'm willing to do it, but I'm trying to poke around for options.
The way I see it, I could either restructure my items from the ground up to load from some sort of database file (which I feel would take away from some of the control I have over the unique function, interactivity, and variety of each given item) for the FormID method, or I could do something else.
The only other ideas I've had so far are adding some sort of FormID manually (which seems like a pain to keep track of), or taking the item name strings from the list of items in the pot, sorting them alphabetically, concatenating them together, and doing a dictionary sweep that way, but I feel like that would be far from ideal. I'm somewhat new to this particular aspect, but I'll do what I can to answer questions.
I recommend the database approach.
At first glance I see it consisting of 3 Tables.
Potion table this is where you would store any intrinsic information that all potions share like a Name, you can also store any traits here in one column using JSON.
Ingredients table this is where you would store all possible ingredients.
Potion_Ingredients table this is a relational table that links any number of ingredients to any number of potions.
Example of a potion in the DB.
ID| NAME |TYPE | EFFECT
1 | Regen Potion |Regen | {"HP":"50","RegenPerTick":".5"}
The Name will be used for display purposes.
The Type will be used by the Consumer to determine how to handle the Potion.
The Effect will be used in context with the Type to act in a general way.
Using JSON in this situation would allow you to store any number of effects per potion.
This post is all over the place, to the point that I can't really tell what you're asking about. You start out talking about crafting and items, so I thought it would be about how you check a list of ingredients against a list of recipes, but then you pivot to traits and deserializing from JSON, but then you provide an example of a healing potion and I can't tell why, then you seem to go back to deserializing and finally back to ingredient matching.
I think it would help you a lot to outline what you're trying to do with a sequence diagram or similar and try to break down your approach into manageable parts. The ingredient matching doesn't have anything to do with deserializing, which in turn shouldn't really have much to do with your possible potion types.
In general, I would urge you to respect encapsulation. When you say:
each of which is their own programmed object with an interface for determining their parent items' function.
that's a big screaming red flag to me. Why would a potion know about a sword or a scythe or a maul or a mace or a halberd etc. etc. A potion just knows it gives +5 to attack for 40 seconds. It's up to the user to know how to use it. Using an attack potion on bracers or boots doesn't do anything because they don't implement the IAttack interface, etc., but because it's an item it still implemented the IItem interface which means it can ApplyPotion(). The potion still gets used, it just gets lost (fizzles).
Traits or modifiers, like a fire opal instead of a regular opal, etc., are just bonuses. They should still count as their base ingredients (opal) for the purposes of ingredient checking, but when you make the potion you aggregate the bonuses on all ingredients and apply them to the finished product. You can have it such that multiple bonuses don't stack, or they do, or they do to an extent, or there could be tiers of bonuses like Fire I, Fire II, etc.
For crafting, you could make a string or int be the key in a dictionary, and you can build that key by assigning each ingredient class its own location, so ones indicates the number of feathers, tens is opals, hundreds is scales, thousands is gold, etc., such that a spell of 1201 would be one piece of gold, two scales, and one feather.
Maybe instead of a dictionary that returns subclasses of Potion you could use a switch statement in a PotionFactory that just configures a single base Potion class. If it doesn't find a match the default case could return null and you fail to build, or you could destroy all the ingredients, etc.
Deserializing is relatively straightforward when you use something like XML if you just make each component responsible for deserializing itself. Load the XML file, and when you get to a Potion tag you instantiate a new potion then hand the XML reference to the Potion. The Potion knows what fields it has and which are required and which are optional. Any classes that Potion needs also deserialize themselves, so each time you hit a Bonus tag you'd instantiate a Bonus, add it to the Potion bonuses, and hand the XML reference off to the Bonus to deserialize whatever it needed. This is almost like a recursive function - it goes as deep as it needs to go.
Anyways this is kind a rambling answer to a rambling question, but hopefully it helps point you in some better directions.
I am using the (new) input system of Unity and have the problem to realise an interaction with different objects. I am still quite new to Unity and hope to find some help.
I have created an event (OnInteract) that listens to whether e has been pressed.
Next I created an object with a collider into which my player can run. Whenever the player meets a certain collider, something should happen. In my case I want to change the scene. However, on my initial scene there are two doors that the player can open by pressing e. I have given both doors the same layer name because they are both exits.
Basically, it works that I can only press e when I hit this particular collider. However, I don't know how to do it instead of performing two different actions. Maybe there is a way to give the objects a script that I can trigger via the PlayerMovement script. Without taking it into the player.
this is my script that works so far:
void OnInteract(InputValue value)
{
if (myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Entrance")))
{
Debug.Log("interact with the door");
}
}
or is there perhaps a way to listen to the "tag" instead of the layer?
I have also come across interfaces, but have not really understood how these help me. Or how I have to use them here to make them work.
In the end you're going to have to check some kind of tag, layer, or reference a component. it's just your preference. To make it a little simpler you can shoot a raycast in the direction you are looking to check for objects instead of having colliders. But in the end it's doing the same thing.
I don't really use interfaces as I'm pretty new myself but from what I can tell they are generally used for organization and global accessibility in similar objects, like doors lol.
I'm making a tile-based train game in C#, and I've decided that the track types should be written as static classes (in my mind, every "Straight" track should exhibit the same behavior, as should every "LargeTurnLeft", "SmallTurnRight", etc.)
The train follows the track by passing a variable "w" into whatever kind of track it is currently riding. Each type of Track has a function called trackLogic that returns different (X,Y) coordinates based on the shape of the track. "trackLogic" is always available for each type of track, but the function differs from type to type (sometimes it returns a straight line, sometimes a curve, maybe a loop-the-loop).
In my mind, the best way to organize this is to have a static, virtual "Track" with functions that are overridden with the specific methods for each kind of track ("LargeTurnRight.trackLogic", etc). This would make making new track types easier. But, as I'm sure y'all already know, static, virtual classes don't exist in C#.
Because this game will eventually involve thousands and thousands of track pieces, I don't want instances of each one. But for multiple track types to have a uniform function structure, they can't be static either.
Should I scrap the individual classes, throw switch cases in the original "Track" class for each type, and update the function with each new piece, or is there a more elegant way to organize multiple functions with the same method name?
I am trying to implement a card game(similar to bridge). There are bunch of classes which represent Suit,Player,Cart,Team,etc.
In each round each player throws 4 cards and once all 13 cards from each player’s hand are thrown, we can calculate points each team of two players collected.
I need to tell the Main form somehow to end (better term is to start a new round ) when all 52 cards are thrown.
I am trying to implement the following pseudo algorithm
While(not all cards played)
{
continue the game
}
Calculate each player’s score according to the collect hand
Not sure where to put this while loop. Certainly not in Form_Load.
Your question is somewhat general, so here is my primary reccommendation:
Don't put business logic in a view class. Your view should react via method calls/events to changes in underlying state. Unfortunately, this is much easier in a tech built around an MVVM/MVC pattern (like WPF).
That being said, I would create a "GameController" class that manages the game state and holds rules such as this. Every time a card is played it would run through a method like:
void CheckRoundEnd()
{
if (cardsPlayed == 52)
EndRound();
}
EndRound would raise an event that the form listened to, or invoke appropriate methods on the form to reset the view for the next round.
I am trying to do the design of a Bejeweled game. I have basically 3 classes. The Game class, which is what is going to be used by the player, a Board class, that represents the board, and a SwitchController class that is responsible for checking if the wanted switch on the board is valid, making the switch, counting the number of possible switches available (so I can know when the game is over, etc).
My current design is something like the following:
Game:
isGameOver()
isSwitchValid(coord1, coord2)
makeSwitch(coord1, coord2)
getPieceAt(coord)
getBoardLength()
IBoard:
getPieceAt(coord)
setPieceAt(coord, piece)
getLength()
My idea would then to have a ISwitchController:
ISwitchController:
isSwitchValid(coord1, coord2)
makeSwitch(coord1, coord2)
getAllValidSwitches()
Here is a little diagram of how the classes are to be organized:
I would have 2 different concrete classes of IBoard available for use (and for each one of them, I'd have to have an ISwitchController implementation).
The problem:
My program is to have 2 IBoard implementations:
The first, ArrayBoard, will have all the pieces of the board stored in a 2D Array. There is nothing special about it. I will define an ArrayBoardSwitchController for managing this class.
The second, ListBoard, will have for each color of pieces, a List/Set with all the coordinates of the pieces of that color. I will define a ListBoardSwitchController for managing this class.
The main issue here is that the implementation of SwitchController will be totally different on ArrayBoard and on ListBoard. For example, while for implementing getAllValidSwitches() ArrayBoardSwitchController only needs the getPieceAt() method, that would not be a good idea to do with ListBoardSwitchController(in that class I use internally lists because it's easier to check if the move is valid that way).
From what I can see, there are 2 different possible solutions:
I could either merge together the
ISwitchController and IBoard
interfaces. That way I'd only have
two classes, Game and Board (while
basically Game would just be a
controller for the Board, as it
would be the Board that had all the
game logic). It wouldn't be that nice
because the classes wouldn't be
as cohese as they could be if I had
3 distinct classes.
Let the interfaces as they are and put
all the methods I need to work with public
in the concrete classes. For example, if I need
a getYellowPiecesList() method, I'd put it public
on ListBoard so ListBoardSwitchController could
use it. ListBoardSwitchController would only
know about it because it knows it only works
against ListBoards.
What's your opinion on the matter? The focus here is not so much on how to design the Bejeweled game, but how to solve this problem, that is recurrent when you try to implement algorithms: on one hand you want to have a clear and good OOP design, and in the other sometimes that gets in the way of having a sound and effective algorithm implementation.
The main issue here is that the implementation of SwitchController will be totally different on ArrayBoard and on ListBoard.
If this is the case, then it sounds like you haven't designed the IBoard interface well enough so that classes can use an implementation of IBoard without knowing the implementation details. If the user of an IBoard needs to know what implementation is being used, then it almost defeats the purpose of having an interface!
I would strongly suggest re-visiting the methods you are exposing on IBoard to see if there is a way you can expose something like "get the piece at this coordinate" in a more generic way. Make sure that any methods a controller needs to invoke on a IBoard instance are only the methods in the IBoard interface.
For example, while for implementing getAllValidSwitches() ArrayBoardSwitchController only needs the getPieceAt() method, that would not be a good idea to do with ListBoardSwitchController(in that class I use internally lists because it's easier to check if the move is valid that way).
If an operation such as "get piece at this coordinate" is instrumental to the IBoard interface, then the implementations must be faithful to their contract and implement it correctly. It sounds as if your ListBoard is not faithfully meeting the contract set out in IBoard.
3: Let ArrayBoardSwitchController and ListBoardSwitchController be inner classes of ArrayBoard and ListBoard. The implementation of the controller is tied to the implementation of your board, thus it makes sense to keep them together. Because the controller will be an inner class you can use the implementation details from the board. Then to make it work extend the IBoard interface to return a ISwitchController.
Note that this is only slightly different from option 1. (The ISwitchController can now be used indirectly from a IBoard, merging them gives direct access to ISwitchController)
What is the purpose of ListBoard as an object decoupled from ArrayBoard? If I were going to bother with the list of gems at all, I would keep it in an object which also held an array of what was in each position, so that swapping the position of two gems could be done quickly and efficiently. Not that I'm clear on why you need the position list anyhow?
If a 6507 running at 1.19Mhz with 128 bytes of RAM and 20% CPU availability can handle Columns, finding all 3-in-a-row combinations on a 6x20 in less than 100ms, I would think more modern machines could scan for moves acceptably fast without using a list of what gems are where. I'd suggest padding your board array so you don't have to worry about edge cases, and for each gem check 16 combinations of various cells within 3 squares of it(*) to see if they both match it. Some moves may be double-reported (e.g. this algorithm may detect that moving a gem left will create a 3-in-a-row, and also detect that moving the gem to the left of the first one right will create a 3-in-a-row) but that shouldn't be a problem.
(*) If a gem can move left, then it must match either the two gems to the left of the destination, or the two gems above the destination, or the two gems below, or one above and one below. Likewise for the other directions.