private void InitializeFSM() { // Create Minion FSM. fsm = new FSM <States>(); IdleState <States> idleState = new IdleState <States>(fsm, this); SearchState <States> searchState = new SearchState <States>(fsm, this); FlockState <States> flockState = new FlockState <States>(fsm, this, _flockingEntity); PursuitState <States> pursuitState = new PursuitState <States>(fsm, this); FleeState <States> fleeState = new FleeState <States>(fsm, this); // [EVERYONE] Idle Transitions. idleState.AddTransition(States.FLOCKING, flockState); idleState.AddTransition(States.SEARCHING, searchState); idleState.AddTransition(States.PURSUIT, pursuitState); pursuitState.AddTransition(States.FLEE, fleeState); pursuitState.AddTransition(States.FLOCKING, flockState); pursuitState.AddTransition(States.IDLE, idleState); fleeState.AddTransition(States.IDLE, idleState); fleeState.AddTransition(States.PURSUIT, pursuitState); // [BOSS] Search Transitions. searchState.AddTransition(States.IDLE, idleState); searchState.AddTransition(States.PURSUIT, pursuitState); // [MINIONS] Flocking Transitions. flockState.AddTransition(States.IDLE, idleState); flockState.AddTransition(States.PURSUIT, pursuitState); fsm.SetInitState(idleState); }
void Awake() { Fsm = new StateMachine <Enemy>(this); State <Enemy> state; switch (this.startState) { case StartState.Wander: state = new WanderState(this, Fsm, 30.0f); break; case StartState.ReachGoal: state = new ReachGoalState(this, Fsm, target); break; case StartState.Flock: state = new FlockState(this, Fsm, target, group); break; case StartState.Flee: state = new FleeState(this, Fsm, target); break; case StartState.FleeMultiple: state = new FleeMultipleState(this, Fsm, group); break; default: state = new WanderState(this, Fsm, 30.0f); break; } Fsm.InitialState(state); }
// Use this for initialization void Start() { // Self references this.dangerHitbox = this.transform.FindChild("Danger Zone").gameObject; this.targetingCone = this.transform.FindChild("Line of Sight").gameObject; _bodyBounds = CreateBodyBounds(); Fsm = new StateMachine <Enemy>(this); State <Enemy> state; switch (this.startState) { case StartState.Wander: state = new WanderState(this, Fsm, 30.0f); break; case StartState.ReachGoal: state = new ReachGoalState(this, Fsm, target); break; case StartState.Flock: state = new FlockState(this, Fsm, target, group); break; case StartState.Flee: state = new FleeState(this, Fsm, target); break; case StartState.FleeMultiple: state = new FleeMultipleState(this, Fsm, group); break; case StartState.FollowPath: Debug.Log("Setting state"); state = new FollowPathState(this, Fsm, PathFinding.spawnGameObjectsAtPathPoints(PathFinding.generatePath(gameObject, target))); Debug.Log("Done setting state"); break; case StartState.SeekTargetWithPath: state = new SeekTargetWithPathState(this, Fsm, target); break; case StartState.Patrol: state = new PatrolState(this, Fsm, patrolPath, patrolPauseTime); break; default: state = new WanderState(this, Fsm, 30.0f); break; } Fsm.InitialState(state); SetDangerous(false); }
// Use this for initialization public override void Start() { manager = GameObject.Find("FlockingManager").GetComponent <FlockingManager>(); targetPosition = GameObject.Find("Target").transform.position; //set the flocking forces to be on separationOn = true; alingmentOn = true; cohesionOn = true; state = FlockState.Standard; base.Start(); }
// Use this for initialization void Start() { _bodyBounds = CreateBodyBounds(); Fsm = new StateMachine <Enemy>(this); State <Enemy> state; switch (this.startState) { case StartState.Wander: state = new WanderState(this, Fsm, 30.0f); break; case StartState.ReachGoal: state = new ReachGoalState(this, Fsm, target); break; case StartState.Flock: state = new FlockState(this, Fsm, target, group); break; case StartState.Flee: state = new FleeState(this, Fsm, target); break; case StartState.FleeMultiple: state = new FleeMultipleState(this, Fsm, group); break; case StartState.FollowPath: Debug.Log("Setting state"); state = new FollowPathState(this, Fsm, PathFinding.spawnGameObjectsAtPathPoints(PathFinding.generatePath(gameObject, target))); Debug.Log("Done setting state"); break; case StartState.SeekTargetWithPath: state = new SeekTargetWithPathState(this, Fsm, target); break; default: state = new WanderState(this, Fsm, 30.0f); break; } Fsm.InitialState(state); }
void SwitchNpcState() { switch (activeNpcState) { case NpcState.PATROL: { anim.SetBool("isIdle", false); anim.SetBool("isWalking", true); anim.SetBool("isAttacking", false); if (Random.Range(0, 10000) < 50) { RandomGoal(); } // flockPrefab.GetComponent<Renderer>().sharedMaterial.color = Color.green; /* * for (int i = 0; i < numFlock; i++) * { * allFlock[i].GetComponent<Renderer>().material.color = Color.green; * } */ activeFlockState = FlockState.PATROL; } break; case NpcState.PURSUE: { // flockPrefab.GetComponent<Renderer>().sharedMaterial.color = Color.yellow; /* * for (int i = 0; i < numFlock; i++) * { * allFlock[i].GetComponent<Renderer>().material.color = Color.yellow; * } */ goalPrefab.transform.rotation = Quaternion.Slerp(goalPrefab.transform.rotation, Quaternion.LookRotation(npcDirection), rotSpeed * Time.deltaTime); goalPrefab.transform.Translate(0, 0, Time.deltaTime * npcSpeed); anim.SetBool("isWalking", true); anim.SetBool("isAttacking", false); // RandomGoal(); activeFlockState = FlockState.PURSUE; } break; case NpcState.ATTACK: { anim.SetBool("isAttacking", true); anim.SetBool("isWalking", false); // flockPrefab.GetComponent<FlockerHead>().changeMaterial(); /* * for (int i = 0; i < numFlock; i++) * { * allFlock[i].GetComponent<Renderer>().material.color = Color.red; * } */ activeFlockState = FlockState.ATTACK; } break; } }
void FixedUpdate() { Vector2 moveDir = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); bool joinButtonDown = Input.GetKey(KeyCode.Space); if (playerControl) { // If there is no move input and the flock state isn't stationary if (flockState != FlockState.Stationary && moveDir == Vector2.zero) { flockState = FlockState.Stationary; SetAgentsHomePoints(); } // If there is move input and the flock state is not moving else if ((moveDir != Vector2.zero || joinButtonDown) && flockState != FlockState.Moving) { flockState = FlockState.Moving; } } var agentPositions = new NativeArray <Vector2>(flockAgents.Count, Allocator.TempJob); var agentVelocities = new NativeArray <Vector2>(flockAgents.Count, Allocator.TempJob); // Populate position and velocity arrays for (int i = 0; i < flockAgents.Count; i++) { agentVelocities[i] = flockAgents[i].velocity; agentPositions[i] = flockAgents[i].transform.position; } // Cant pass the camera (reference type) into the job, so instead pass // the information the job needs from the camera float height = boundCamera.orthographicSize * 2.0f; var jobHandles = new List <JobHandle>(); for (int i = 0; i < flockAgents.Count; i++) { UpdateBoidJob updateBoidJob = new UpdateBoidJob { index = i, startPosition = transform.position, attractToStart = attractToHome, playerControl = playerControl, homePoint = flockAgents[i].HomePoint, flockState = flockState, cameraHeight = height, cameraWidth = height * Screen.width / Screen.height, cameraPosition = boundCamera.transform.position, moveDirection = moveDir, sightRadius = joinButtonDown ? float.MaxValue : sightRadius, avoidanceRadius = avoidanceRadius, velocityLimit = velocityLimit, percentGravity = percentageGravity, percentMatchVelocity = percentageMatchVelocity, percentAvoidance = percentageAvoidance, percentBoundForce = percentageBoundForce, percentMouseForce = Random.Range(0, 11) % 10 == 0 ? percentageMouseForce : 0, velocities = agentVelocities, positions = agentPositions }; jobHandles.Add(updateBoidJob.Schedule()); } for (int i = 0; i < flockAgents.Count; i++) { jobHandles[i].Complete(); flockAgents[i].velocity = agentVelocities[i]; flockAgents[i].transform.position += new Vector3(flockAgents[i].velocity.x, flockAgents[i].velocity.y, 0); } UpdateAveragePosition(); agentPositions.Dispose(); agentVelocities.Dispose(); }
// Update is called once per frame void Update() { float t = getTime(); bool moveToState = false; if (state != nextState) { stateStartedTime = t; state = nextState; moveToState = true; } float stateElapsed = t - stateStartedTime; switch (state) { case FlockState.ground: { foreach (var bird in birds) { bird.offset = Vector2.zero; bird.trans.localPosition = bird.originalPosition; bird.trans.GetComponent <Animator>().SetInteger("animState", 0); } if (stateElapsed > groundDuration) { nextState = FlockState.rise; } } break; case FlockState.rise: { foreach (var bird in birds) { bird.offset += new Vector2(0, Time.deltaTime * 4f); bird.trans.localPosition = bird.originalPosition + bird.offset; bird.trans.GetComponent <Animator>().SetInteger("animState", 1); } if (stateElapsed > riseDuration) { nextState = FlockState.fly; } } break; case FlockState.fly: { for (int i = 0; i < birds.Count; ++i) { BirdState bird = birds[i]; bird.offset += new Vector2(Mathf.Sin(t * 4 + i) * 0.018f, Mathf.Sin(t * 2.4f + i) * 0.04f); bird.trans.localPosition = bird.originalPosition + bird.offset; } if (stateElapsed > flyDuration) { nextState = FlockState.land; } } break; case FlockState.land: { foreach (var bird in birds) { bird.offset -= bird.offset * landDuration * Time.deltaTime; bird.trans.localPosition = bird.originalPosition + bird.offset; } if (stateElapsed > landDuration) { nextState = FlockState.ground; } } break; } }