public override void Update(float dt) { foreach (Entity entity in _entities) { PaddleComponent paddleComp = entity.GetComponent <PaddleComponent>(); PlayerComponent playerComp = entity.GetComponent <PlayerComponent>(); TransformComponent transformComp = entity.GetComponent <TransformComponent>(); Vector2 delta = Vector2.Zero; delta.Y += Constants.Pong.PADDLE_SPEED * playerComp.Input.GetAxis() * dt; transformComp.Move(delta); if (transformComp.Position.Y + paddleComp.Bounds.Y / 2 > Constants.Pong.PLAYFIELD_HEIGHT / 2) { transformComp.SetPosition(transformComp.Position.X, Constants.Pong.PLAYFIELD_HEIGHT / 2 - paddleComp.Bounds.Y / 2); } if (transformComp.Position.Y - paddleComp.Bounds.Y / 2 < -Constants.Pong.PLAYFIELD_HEIGHT / 2) { transformComp.SetPosition(transformComp.Position.X, -Constants.Pong.PLAYFIELD_HEIGHT / 2 + paddleComp.Bounds.Y / 2); } } }
public void Init(PaddleComponent data) { this.data = data; rb = GetComponent <Rigidbody2D>(); Message.AddListener("RestartLevel", Restart); Message.AddListener("StartLevel", StartLevel); }
///<summary> /// Use this for initialization ///</summary> override protected void Start () { base.Start(); _paddleComponent = GetComponent<PaddleComponent>(); }
// PUBLIC // PRIVATE // PRIVATE STATIC // PRIVATE COROUTINE // PRIVATE INVOKE //-------------------------------------- // Events //-------------------------------------- /// <summary> /// _ons the user interface collision enter2 D signal. /// </summary> /// <param name="aTag_string">A tag_string.</param> private void _onUICollisionEnter2DSignal(GameObject aCollidedWith_gameobject) { //WHAT DID WE HIT? switch (aCollidedWith_gameobject.tag) { case CPUPaddleUI.TAG: case PlayerPaddleUI.TAG: // PaddleComponent paddleComponent = aCollidedWith_gameobject.GetComponent <PaddleComponent>(); view.doHandleCollisionWithPaddle(paddleComponent.velocity); paddleComponent.doSpinOnce(); // soundPlaySignal.Dispatch(new SoundPlayVO(SoundType.PADDLE_HIT)); break; case BoundaryComponent.TAG: //WHICH TYPE OF BOUNDARY WAS HIT? BoundaryComponent boundaryComponent = aCollidedWith_gameobject.GetComponent <BoundaryComponent>(); // switch (boundaryComponent.boundaryType) { case BoundaryType.LeftGoal: //LEFT BOUND HIT? THEN REWARD RIGHT SCORE rightPaddleScoreChangeSignal.Dispatch(1); soundPlaySignal.Dispatch(new SoundPlayVO(SoundType.GOAL_LOSS)); break; case BoundaryType.RightGoal: //RIGHT BOUND HIT? THEN REWARD LEFT SCORE leftPaddleScoreChangeSignal.Dispatch(1); soundPlaySignal.Dispatch(new SoundPlayVO(SoundType.GOAL_WIN)); break; case BoundaryType.None: soundPlaySignal.Dispatch(new SoundPlayVO(SoundType.PADDLE_HIT)); break; default: #pragma warning disable 0162 throw new SwitchStatementException(); break; #pragma warning restore 0162 } break; } }
// PRIVATE STATIC //-------------------------------------- // Methods //-------------------------------------- ///<summary> /// Use this for initialization ///</summary> override protected void Start() { base.Start(); // cpuPaddleComponent = cpuPaddle_gameobject.GetComponent <PaddleComponent>(); playerPaddleComponent = playerPaddle_gameobject.GetComponent <PaddleComponent>(); //WE DISABLE THE INTRO TO EASE THE USE OF THE UNITY IDE //HERE WE ENABLE IT introUIGameObject.SetActive(true); // StartCoroutine(Wait_ThenAllViewsInitializedSignal()); }
public override void Update(float dt) { if (_ballEntities.Count > 0) { // TODO: Some sort of voting system if there are multiple balls Entity ball = _ballEntities[0]; TransformComponent ballTransform = ball.GetComponent <TransformComponent>(); BallComponent ballComp = ball.GetComponent <BallComponent>(); for (int i = 0; i < _aiPaddleEntities.Count; i++) { Entity ai = _aiPaddleEntities[i]; AIComponent aiComp = ai.GetComponent <AIComponent>(); PaddleComponent aiPaddleComp = ai.GetComponent <PaddleComponent>(); TransformComponent aiTransform = ai.GetComponent <TransformComponent>(); aiComp.AIPlayer.AIInputMethod.Think(aiTransform.Position, aiPaddleComp.Normal, ballTransform.Position, ballTransform.Position - ballTransform.LastPosition, ball); } } }
void processCollision(Entity ballEntity) { TransformComponent transformComp = ballEntity.GetComponent <TransformComponent>(); BallComponent ballComp = ballEntity.GetComponent <BallComponent>(); // Wrap ball angle while (ballComp.Direction < 0) { ballComp.Direction += 2 * MathHelper.Pi; } while (ballComp.Direction > 2 * MathHelper.Pi) { ballComp.Direction -= 2 * MathHelper.Pi; } BoundingRect ballAABB = new BoundingRect(transformComp.Position - ballComp.Bounds / 2, transformComp.Position + ballComp.Bounds / 2); // Paddles foreach (Entity paddleEntity in _paddleEntities) { PaddleComponent paddleComp = paddleEntity.GetComponent <PaddleComponent>(); TransformComponent paddleTransformComp = paddleEntity.GetComponent <TransformComponent>(); BoundingRect paddleAABB = new BoundingRect(paddleTransformComp.Position - paddleComp.Bounds / 2, paddleTransformComp.Position + paddleComp.Bounds / 2); if (ballAABB.Intersects(paddleAABB)) { if (!paddleComp.IgnoreCollisions) { { Vector2 ballEdge = transformComp.Position + ballComp.Bounds * -paddleComp.Normal; Vector2 paddleEdge = paddleTransformComp.Position + paddleComp.Bounds * paddleComp.Normal; Vector2 bouncePosition = (ballEdge + paddleEdge) / 2; EventManager.Instance.QueueEvent(new BallBounceEvent(ballEntity, paddleEntity, bouncePosition)); } // Determine alpha of ball relative to the paddle's heigh float relativeY = transformComp.Position.Y - paddleTransformComp.Position.Y; float alpha = (relativeY + (paddleComp.Bounds.Y / 2)) / paddleComp.Bounds.Y; alpha = MathHelper.Clamp(alpha, 0, 1); // Determine new direction float newDir = MathHelper.Lerp(Constants.Pong.PADDLE_BOUNCE_MIN, Constants.Pong.PADDLE_BOUNCE_MAX, alpha); newDir = MathHelper.ToRadians(newDir); // Set ball direction if (paddleComp.Normal.X > 0) { ballComp.Direction = newDir; } else if (paddleComp.Normal.X < 0) { ballComp.Direction = MathHelper.Pi - newDir; } // Make sure ball does not get stuck inside paddle paddleComp.IgnoreCollisions = true; } } else { paddleComp.IgnoreCollisions = false; } } // Edges foreach (Entity edgeEntity in _edgeEntities) { EdgeComponent edgeComp = edgeEntity.GetComponent <EdgeComponent>(); TransformComponent edgeTransformComp = edgeEntity.GetComponent <TransformComponent>(); BoundingRect edgeAABB = new BoundingRect(edgeTransformComp.Position.X - Constants.Pong.PLAYFIELD_WIDTH / 2, edgeTransformComp.Position.Y - Constants.Pong.EDGE_HEIGHT / 2, Constants.Pong.EDGE_WIDTH, Constants.Pong.EDGE_HEIGHT); if (ballAABB.Intersects(edgeAABB)) { { Vector2 ballEdge = transformComp.Position + ballComp.Bounds * -edgeComp.Normal; Vector2 edgeEdge = edgeTransformComp.Position + new Vector2(Constants.Pong.EDGE_WIDTH, Constants.Pong.EDGE_HEIGHT) * edgeComp.Normal; Vector2 bouncePosition = (ballEdge + edgeEdge) / 2; EventManager.Instance.QueueEvent(new BallBounceEvent(ballEntity, edgeEntity, bouncePosition)); } // Determine directional vector of ball Vector2 ballDirection = new Vector2((float)Math.Cos(ballComp.Direction), (float)Math.Sin(ballComp.Direction)); // Determine reflection vector of ball Vector2 ballReflectionDir = getReflectionVector(ballDirection, edgeComp.Normal); // Set angle of new directional vector ballComp.Direction = (float)Math.Atan2(ballReflectionDir.Y, ballReflectionDir.X); } } }