处理对象之间的交互 [英] Handling interaction between objects

查看:122
本文介绍了处理对象之间的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在团结一致地制作一些游戏.我正在使用C#.通常,我对游戏中的代码感到满意,并且我知道如何使其变得优雅".我非常擅长编码单个元素(例如,小行星中的太空飞船).但是我只对自己的代码感到满意,直到达到一个对象需要与另一个对象进行交互的程度为止.在那之后,它变成了代码的意粉,并且我总是放弃该项目..我还没有找到一种优雅的方式来处理事情.我相信我已经在互联网上的各个地方问过,但是我似乎一直在回头.

I am currently working in unity to make a few games. I am using C#. I am usually happy with the code in my game and i know how to make it 'elegant', so to speak. I am very good at coding individual elements (Say, the spaceship in Asteroids). But I am only happy with my code until I get to the point where one object needs to interact with another. It becomes a spaghetti of code after that point and i ALWAYS drop the project.. I have yet to find a graceful way of handling things. I believe I have asked in various places around the internet, but I seem to keep coming back to this.

是否有任何通用的方法来处理对象之间的交互?有什么不客气的东西吗?这个问题在我的最新项目中再次出现.这是一个Unity项目,是2D侧滑车.检测我是否用角色击中了一个尖峰",这涉及检查碰撞对象标签并查看它是否是一个尖峰".但是然后我所有的死亡"代码都包含在播放器中,而不是峰值.除了带有标签的网状对撞机之外,尖峰实际上并不是什么.这感觉不对.

Is there any common method of handling the interactions between objects? anything that doesn't feel hacky? This problem arose again in my latest project. It is a Unity project, a 2d sidescroller. Detecting whether or not I hit a 'spike' with my character involves checking the collision objects tag and seeing whether or not it is a 'spike'. But then all my 'death' code is contained within the Player, not the spike. The spike isn't really anything other than a mesh collider with a tag. and this feels wrong.

那么stackoverflow,你们所有人如何处理呢?

So stackoverflow, how do you all handle this?

推荐答案

Johnn Blade在问题注释中使用观察者模式可能是一种在应用程序不同部分之间传递状态变化的好方法.

As Johnn Blade left in a comment on the question, using the Observer Pattern is probably a good approach for communicating state changes between different parts of your application.

作为一个具体的示例,您可以将播放器的移动分解为几个离散的步骤(或事件),这些步骤可以由应用程序的其他部分观察到.

As a concrete example, you may be able to break player movement down into several discrete steps (or events) that can be observed by other parts of the application.

例如,如果您将以下事件添加到播放器:

For example, if you add the following events to the player:

  • BeforeMove(坐标oldCoordinate,坐标newCoordinate,输出布尔canMove)
  • 已移动(坐标oldCoordinate,坐标newCoordinate)
  • HealthChanged(int newHealth)

场景中的其他对象可能会发生以下事件:

And other objects in the scene may have events such as:

  • DamagePlayer(伤害)
  • HealPlayer(int heal)

当您要移动玩家时,您将触发BeforeMoved事件-如果没有任何响应canMove = false(例如,锁着的门),则允许移动.然后,您更新玩家的位置,并调用已移动".

When you are about to move a player, you would trigger the BeforeMoved event - if nothing responds with canMove = false (eg a locked door), then the move is permitted. Then you update the player's position, and call Moved.

因此,您的Spike可能会收听玩家移动事件:

So, your Spike may listen for player Moved events:

void Moved(Coordinate oldCoordinate, Coordinate newCoordinate) 
{ 
  if (newCoordinate.Intersects(this.Location))
  {
    DamagePlayer(50); 
  }
} 

相应地,您的播放器将侦听DamagePlayer事件.

Correspondingly your player will listen for DamagePlayer events.

void DamagePlayer(int damage)
 {
    this.Health -= damage;
    HealthChanged(this.Health);  
 }

某些东西(可能在播放器本身上)会侦听HealthChanged事件,当事件达到零或更少时,会杀死播放器.

Something (possibly on the player itself) would listen for HealthChanged events, and when it reaches zero or less, kills the player.

使用此模式,添加新功能(例如检测跌倒)相对简单.只需为Moved事件创建一个新的观察者:

Using this pattern, adding new functionality such as detecting falling is relatively simple. Just create a new observer of the Moved event:

void Moved(Coordinate oldCoordinate, Coordinate newCoordinate) 
{ 
  decimal deltaY = oldCoordinate.Y - newCoordinate.Y; 
  if (deltaY  < -100) // If fell 100 units or more, take 10 damage per unit. 
  {
    DamagePlayer(10 * Math.Abs(deltaY)); 
  }
} 

这篇关于处理对象之间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆