/// <summary> /// ボール発射 /// </summary> public void InitBall() { currentBall = Util.InstantiateComponent <BallComponent>(this.prefabBall, this.parentBall); currentBall.Init(this.CollisionEnter); ballList.AddLast(currentBall); }
void processMovement(Entity ballEntity, float dt) { TransformComponent transformComp = ballEntity.GetComponent <TransformComponent>(); BallComponent ballComp = ballEntity.GetComponent <BallComponent>(); transformComp.Move((float)Math.Cos(ballComp.Direction) * ballComp.Velocity * dt, (float)Math.Sin(ballComp.Direction) * ballComp.Velocity * dt); }
public IEnumerator ApplyPowerUp(GameObject Other) { PaddleController Paddle = Other.GetComponent <PaddleController>(); // If the other component alread has a power up destroy the power up now if (Paddle.HasPowerUp) { Destroy(gameObject); yield return(null); } // GetComponent <SpriteRenderer>().enabled = false; GetComponent <BoxCollider2D>().enabled = false; switch (PowerUpType) { case Type.LARGE_PADDLE: Paddle.HasPowerUp = true; Other.transform.localScale = new Vector3(100f, 200f); yield return(new WaitForSeconds(PowerUpTime)); Paddle.HasPowerUp = false; Other.transform.localScale = new Vector3(100f, 100f); break; case Type.MAGNETIC_PADDLE: Paddle.IsMagnetic = true; Paddle.HasPowerUp = true; Other.GetComponent <ColorModifier>().ModifierMode = ColorModifier.ColorMode.FIXED; Other.GetComponent <ColorModifier>().SetColor(new Color(1, 1, 1)); yield return(new WaitForSeconds(PowerUpTime)); Paddle.IsMagnetic = false; Paddle.HasPowerUp = false; GameObject.Find("Ball").GetComponent <BallComponent>().CurrentStatus = BallComponent.Status.Moving; Other.GetComponent <ColorModifier>().ModifierMode = ColorModifier.ColorMode.TIME_BASED; break; case Type.STRONG_BALL: BallComponent Ball = GameObject.Find("Ball").GetComponent <BallComponent>(); ColorModifier BallColor = GameObject.Find("Ball").GetComponent <ColorModifier>(); Paddle.HasPowerUp = true; Ball.isOnFire = true; BallColor.ModifierMode = ColorModifier.ColorMode.FIXED; BallColor.SetColor(new Color(1f, 0f, 0f)); yield return(new WaitForSeconds(PowerUpTime)); Paddle.HasPowerUp = false; BallColor.ModifierMode = ColorModifier.ColorMode.TIME_BASED; Ball.isOnFire = false; break; } // Destroy the object Destroy(gameObject); }
public void Init(BallComponent data) { this.data = data; rb = GetComponent <Rigidbody2D>(); Restart(); Message.AddListener("RestartLevel", Restart); Message.AddListener("StartLevel", StartLevel); }
private void Enemy() { if (this.pongCtrl.ballList.Count.Equals(0)) { return; } BallComponent ball = this.pongCtrl.ballList.First.Value; this.transform.position = new Vector3(this.transform.position.x, ball.transform.position.y, 0f); }
/// <summary> /// ラケットを自動で動かす /// </summary> /// <param name="ballList">Ball list.</param> private void Enemy(LinkedList <BallComponent> ballList) { if (ballList == null || ballList.Count == 0) { return; } BallComponent ball = ballList.Last.Value; this.transform.position = new Vector3(this.transform.position.x, ball.transform.position.y, this.transform.position.z); }
void HandleBallBounce(BallBounceEvent ballBounceEvent) { // Only if it is a paddle if (ballBounceEvent.Collider.HasComponent <PaddleComponent>()) { foreach (Entity ballEntity in _ballEntities) { BallComponent ballComp = ballEntity.GetComponent <BallComponent>(); ballComp.Velocity += Constants.Pong.BALL_SPEED_INCREASE; } } }
public void Think(Vector2 pos, Vector2 nor, Vector2 ballPos, Vector2 ballVel, Entity ball) { paddleX = pos.X; _snapshot._axis = 0; switch (_state) { case State.Idle: { bool withinDistance = Math.Abs(pos.X - ballPos.X) <= Constants.AI.ACTIVE_DISTANCE; bool opposingNormals = Vector2.Dot(nor, ballVel) < 0; if (withinDistance && opposingNormals) { TransformComponent acBallTransform = ball.GetComponent <TransformComponent>(); BallComponent acBallComp = ball.GetComponent <BallComponent>(); ballTransform.SetPosition(acBallTransform.Position); ballTransform.SetRotation(acBallTransform.Rotation); ballComponent.Bounds = new Vector2(acBallComp.Bounds.X, acBallComp.Bounds.Y); ballComponent.Direction = acBallComp.Direction; ballComponent.Velocity = acBallComp.Velocity; _state = State.Predicting; } } break; case State.MovingToTarget: { if (pos.Y > yTarget) { _snapshot._axis = -1; } else { _snapshot._axis = 1; } bool opposingNormals = Vector2.Dot(nor, ballVel) < 0; if (Math.Abs(pos.Y - yTarget) < Constants.AI.TARGET_SENSITIVITY || !opposingNormals) { _snapshot._axis = 0; _state = State.Idle; } } break; } }
/// <summary> /// ボールがゴールに衝突 /// </summary> /// <param name="ball">Ball.</param> /// <param name="goal">Goal.</param> public void CollisionEnter(BallComponent ball, GoalComponent goal) { this.scoreMgr.AddScore(goal.playerId); this.UpdateScoreLabel(); if (this.scoreMgr.isGameSet) { this.GameSet(); } else { this.InitBall(); } }
public AIInputMethod() { engine = new Engine(); ballSystem = new BallMovementSystem(engine); ball = engine.CreateEntity(); ballTransform = new TransformComponent(); ballComponent = new BallComponent(0); ball.AddComponent(ballTransform); ball.AddComponent(ballComponent); // Edges EdgeEntity.Create(engine, null, new Vector2(0, Constants.Pong.PLAYFIELD_HEIGHT / 2), new Vector2(0, -1)); EdgeEntity.Create(engine, null, new Vector2(0, -Constants.Pong.PLAYFIELD_HEIGHT / 2), new Vector2(0, 1)); }
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); } } }
public override void Update(float dt) { foreach (Entity goalEntity in _goalEntities) { TransformComponent goalTransformComp = goalEntity.GetComponent <TransformComponent>(); BoundingRect goalAABB = new BoundingRect(goalTransformComp.Position.X - Constants.Pong.GOAL_WIDTH / 2, goalTransformComp.Position.Y - Constants.Pong.PLAYFIELD_HEIGHT / 2, Constants.Pong.GOAL_WIDTH, Constants.Pong.PLAYFIELD_HEIGHT); foreach (Entity ballEntity in _ballEntities) { TransformComponent ballTransformComp = ballEntity.GetComponent <TransformComponent>(); BallComponent ballComp = ballEntity.GetComponent <BallComponent>(); BoundingRect ballAABB = new BoundingRect(ballTransformComp.Position - ballComp.Bounds / 2, ballTransformComp.Position + ballComp.Bounds / 2); if (ballAABB.Intersects(goalAABB)) { Vector2 goalNormal = goalTransformComp.Position - ballTransformComp.Position; goalNormal.Normalize(); Vector2 ballEdge = ballTransformComp.Position + ballComp.Bounds * -goalNormal; Vector2 goalEdge = goalTransformComp.Position + new Vector2(Constants.Pong.GOAL_WIDTH, Constants.Pong.GOAL_HEIGHT) * goalNormal; Vector2 goalPosition = (ballEdge + goalEdge) / 2; EventManager.Instance.QueueEvent(new GoalEvent(ballEntity, goalEntity, goalPosition)); } } } }
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); } } }
/// <summary> /// ボール発射 /// </summary> public void InitBall() { BallComponent ball = Util.InstantiateComponent <BallComponent>(this.prefabBall, this.parentBall); ball.Init(this); }