Modification to Unity2D Roguelike
Hello all. I followed through with the Unity2D tutorial on creating a roguelike game, though not so much because I was creating a game like it, but rather that I was attempting to find a simple solution to creating a world based on tiles. It works out very well for what is intended, however there is one big issue that's blocking me from being able to generalize the system.
To define interactions with an object, they use this block of code, which also refers to other Movable Object (check out this part of the code to figure out what I mean). First is the player, second is the enemy.
Player:
protected override void OnCantMove<T>(T component)
{
Wall hitWall = component as Wall;
hitWall.DamageWall(wallDamage);
}
Enemy:
protected override void OnCantMove<T>(T component)
{
PlayerTileMove hitPlayer = component as PlayerTileMove;
hitPlayer.LoseHealth(playerDamage);
}
Again, works well for the purposes of demonstration, but what I found is that you cannot define any secondary interactions within this code. I wanted my enemies to have the ability to knock down walls so I added a Debug.Log to find the "component" tag, but it only recognizes the tag for the object it can directly interact with (Player with walls, Enemies with Player).
Essentially it only recognizes the object which it can preform special actions on. This may be down to how the colliders work in each script (player movement and enemy movement).
Enemy Should Destroy Blocks:
if (component.tag == "Wall")
{
Wall hitWall = component as Wall;
hitWall.DamageWall(4);
}
Player Should Damage Enemy:
if (component.tag == "Enemy")
{
EnemyTileMove hitEnemy = component as EnemyTileMove;
hitEnemy.LoseHealth(5);
}
Can anyone explain why the OnCantMove function can only check for one type of object and/or how to get around that? Thanks for your time!
@GoodGuyA isn't it the case that in the Player "Update"-method the following line is written Attempt$$anonymous$$ove<Wall> (horizontal, vertical);
-> When you check in the Attempt$$anonymous$$ove base method (in the $$anonymous$$ovingObject class) there you can see that OnCant$$anonymous$$ove is called with the type which was given in Attempt$$anonymous$$ove. Because in Player script there is "Wall" passed to this method the player only reacts on walls.
Answer by TheBonZi · Apr 01, 2016 at 08:37 PM
How easy way to see what we should be given in the "Attempt Move (horizontal, vertical);" Sorry for digging up an old post.