How to implement the Command Pattern in a Puzzle Game - c#

Setup:
I'm making a classic top-down, tile based puzzle game about pushing blocks and I'm trying to implement the Command Pattern to have a Undo/Redo function. In my design, a "turn" is fired whenever the player moves one tile, and the world must update somehow (lasers can be blocked, Blocks are destroyed if on top of water, Big floor buttons are pressed down, etc.)
I've abstracted my player movement into a Concrete Command MoveCommand by simply changing its position based on a direction on execution. This MoveCommand is then instantiated inside a Move() method. Blocks also happen to have a Move() method that must be called whenever the player "pushes" them (i.e. walks into them and there's a free space behind).
Question 1. Should the Block movement be its own Command, or should it be somehow appended to and managed by the player's MoveCommand? Right now I figured I would have each be its own separate thing, and then whenever the player presses the Undo button, have the Undo method execute several times until everything that must happen in that turn resolves. However, I'm not sure whether this is how you're supposed to be implementing this pattern.
Question 2 As mentioned above, blocks must be destroyed when they touch water. Should this destruction be its own WalkIntoDeadlyTile Command, or should it be appended to the Block's MoveCommand? Both questions make me realize I might not be understanding how to use this pattern, but I'm not sure how to look it up or answer these specific problems.

It is a rare case, where the ICommand interface would not be completely missplaced. You can not keep polling CanExecute in most environments. You need a way to force re-evaluation on conditions. Or just use something with binding.
But games are what I call "notorious redrawers" anyway. They could poll CanExecute once per draw pass and once in the Input processing pass, without much issues. The check would also be relatively simple (isPlayerTurn). The only possible issue is getting teh games state into the Command Implementations.
Question 2 As mentioned above, blocks must be destroyed when they touch water. Should this destruction be its own WalkIntoDeadlyTile Command, or should it be appended to the Block's MoveCommand?
It should happen in the colission check. A basic square one would work, If the block box collides with the water box, call destroy on the block. This also prevents you from forgetting to add special movement to later blocks or if the blocks somehow teleport below water.

Related

How can I make two animations work together in unity?

I'm making a first person shooter and im trying to make a reload animation.
I want to make it look like the hand grabs the mag and pulls it down.
How do I make two animations work together?
I remember having to do that for my wrestling game. What I did was create 2 controllers and 2 animations. Controllers should be the same (have same timing, transition and all that). Then make one animation for example animation of how hand reloads a gun. Now memorize sample rate and in which frame you put keys and what do they do (what do they change). Now start animating how mag is getting pulled BASED on what you have done previously. Like in what frame it should go down and in which frame it should be thrown or new one should be inserted and etc.
Then you will have to enter play mode in order to see them simultaneously. I am making a wrestling game and this method is what I use.
Good luck
https://docs.unity3d.com/Manual/AnimatorOverrideController.html
he Animator Override Controller is a type of asset which allows you to extend an existing Animator Controller
, replacing the specific animations used but otherwise retaining the original’s structure, parameters and logic.
This allows you to create multiple variants of the same basic state machine
, but with each using different sets of animations. For example, your game may have a variety of NPC types living in the world, but each type (goblin, ogre, elf, etc) has their own unique animations for walking, idling, sitting, etc.
By creating one “base” Animator Controller containing the logic for all NPC types, you could then create an override for each type and drop in their respective animation files.

How to make dynamic shape adjustment to another shape

Hi guys i was wondering how to create a shape adjustment with two objects which specifically could be described as the independent cells, one of which is static, and the second one is dynamic and surrounded by "plasma". The movement of the active object must be controllable by the user (WSAD). Collision of the active object with the static one causes the static object to be swallen, though doesn't change it's position stays in place all the time. As the active object moves, passes the swallen object and troughts it out.
See the image below:
Player character
When it comes close enough to pink enemy it's starting to swallow it (surround by yellow thing)
Pink enemy is completely sourrounded when red circle is in the centre of both.
When it leaves enemy it takes off the yellow thing
I was wondering what is the simplest way to do it. I've been thinking about cloth, physics joints, mesh substraction (is it even possible?), some kind of animation... I don't have much time to do it. Can you show me the simplest way. Which tools and approach should i use? I'm not asking for full code or full solution only for some tips.
Tim Hunter mentioned a wonderful way, most perfect in 3D.
You can use another approach in 2D :
Inside OnCollisionEnter2D try finding hit points using Collision2D.contacts . See this reference .
Create some particle effect there.
Disable the enemy
Now play swallowing animation of the player.
At animation end, enable enemy again.
Maybe calculation is little tricky, still efficient.

WPF animation, check for overlapping?

NOTE: Please be sure to read the Let us continue this discussion in chat for background notes
I have seen a few posts on here and tutorials for checking to see if controls overlap but what is the best method for checking this during an animation?
I'm making a simulation software which involves having some UIElements animate along a path. At the moment I have 20 items following this path and it works fine.
To do this I just create a loop of 20 and inside the loop, I am creating the UIElement, storyboard etc and then starting. I wait for about 100ms then repeat. This gives a nice gap between the elements.
Anyway, the above works as it should. Now the next bit. At any point of the sim, a UIElement can stop where it is. Now when this happens, I want the other UIElements to keep going until they hit the stopped element, and 1 by 1 they stack up behind it.
So knowing how to check for overlap/intersection/collision, how do I check during an animation of elements.
The collision detection, by its nature, needs a constantly checking timer. because there are no collision events in WPF.
You can check for the collision of two Rect objects with rect1.IntersectsWith(rect2)
taken from here
You can find the Rect of an element relative to its parent using
public static Rect BoundsRelativeTo(this FrameworkElement element,
Visual relativeTo)
{
return
element.TransformToVisual(relativeTo)
.TransformBounds(LayoutInformation.GetLayoutSlot(element));
}
taken from here
The question is how many elements are being dropped into the bottleneck of the timer at a time, if the collision detection ALWAYS involves a stopped element, then that element is responsible for collision detection. Otherwise, you will need to check n^2 collisions
(which is bad). You can work on the efficiency of this bottleneck by reducing the number of comparisons as much as possible. For example, since the objects are moving along a common path you can compare the stopped element against its previous element only. You can ensure a sorted collection of elements to achieve that.
Since all elements are following a common path (one collision occurs at a time):
start the timer when the first element is stopped. and set it as the reference element
compare all required elements against the reference element
change the reference element when a collision occurs to the collided element

Detect Input.GetMouseButtonUp outside a GameObject in Unity?

How do I detect Input.GetMouseButtonUp outside a specific GameObject's area in Unity? Are there any Unity Assets for this, or should we do this programmatically?
It certainly has to be done by programming it, whether there is an asset or not. As it is rather easy to program, I doubt there is one.
What you need to understand is, that Input.GetMouseButtonUp(int button) is an event and entirely decoupled from any "position". The description from the API says "Returns true during the frame the user releases the given mouse button.".
So in the frame a mouse button has been released you want to check if the arrow is "touching" or laying over some GameObject (GO). According to your example you will do nothing when the GO was hit, and else do something.
One way to do that is to use the following method:
http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Your GameObject will at least need a collider, maybe a rigidbody too in order to be detected by the ray. I'm not completely sure atm if both are needed.
Additional note: Having an UI element, this method could also be used to set some flag while hovering over it e.g. : http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html

Somehow physically colliding with Collision Triggers

With a whole bunch of copy-pasting from all over the web, I managed to hack together a piece of C# code which spawns a random object from an array when the player enters a large, spherical collision trigger. When the player leaves the trigger, the object is deleted. It's working almost as intended, only problem is that six out of ten times when the player passes through the collider, for a split second it feels like a physical collider. Meaning that although the player is trying to move straight forward, he or she will move left or right along the trigger collider for about half a second, before the object spawns and the player can move freely again.
I was under the impression that checking the "Is Trigger" box would remove all physical properties of that specific collider? Am I missing something?
I'm using the standard FPS Character Controller in Unity 5 if that might have something to do with it.
All help is greatly appreciated.
Make sure the spherical collision trigger has a rigid body component, and that the "Is Kinematic" property of the rigid body is checked.
Realized my solution didn't get posted as I thought it had. Yoyo's solution was correct. The object containing the code was also the trigger. Switched so that the player was the trigger and now all is well.

Categories