usingUnityEngine;usingSystem.Collections;usingSystem.Collections.Generic;publicclassDragonMovement:MonoBehaviour{publicList<Renderer>solids;publicGameObjecthurtPrefab;publicfloathorizontalSpeed=0.1f;publicfloatverticalSpeed=0.2f;privateVector3direcV=Vector3.zero;privateVector3deltaV=Vector3.zero;publicfloathorizontalBound=6f;publicfloatverticalBound=4f;privatefloatslitherPhase=0;publicHealthBarhealthBar;privatefloathurtCooldown=0;privateSwarmSystemswarm;privateenumState{normal,swirl,slim,spread};privateStatestate=State.normal;privatevoidapplyMotion(Vector3direc){direcV=direcV+direc;}privatevoidmoveUp(){applyMotion(Vector3.up*verticalSpeed);}privatevoidmoveDown(){applyMotion(Vector3.down*verticalSpeed);}privatevoidmoveLeft(){applyMotion(Vector3.left*horizontalSpeed);}privatevoidmoveRight(){applyMotion(Vector3.right*horizontalSpeed);}privatevoidturnVisible(boolvisible){foreach(Renderersolidinsolids){solid.enabled=visible;}}privateIEnumeratortransformSlim(){while(state==State.slim&&swarm.Collapse<0.8f){swarm.Collapse+=0.05f;yieldreturnnewWaitForSeconds(0.01f);}}privateIEnumeratortransformSpread(){while(state==State.spread&&swarm.Noise<3){swarm.Noise+=0.05f;yieldreturnnewWaitForSeconds(0.01f);}}privateIEnumeratortransformAntiSlim(){while(state==State.normal&&swarm.Collapse>0){swarm.Collapse-=0.07f;yieldreturnnewWaitForSeconds(0.01f);}if(state==State.normal){turnVisible(true);swarm.activate(false);}}privateIEnumeratortransformAntiSpread(){while(state==State.normal&&swarm.Noise>0){swarm.Noise-=0.12f;yieldreturnnewWaitForSeconds(0.01f);}if(state==State.normal){turnVisible(true);swarm.activate(false);}}privatevoidturnSwirl(){resetTurn();turnVisible(false);swarm.activate(true);state=State.swirl;}privatevoidturnSlim(){resetTurn();turnVisible(false);swarm.activate(true);state=State.slim;StartCoroutine("transformSlim");}privatevoidturnSpread(){resetTurn();turnVisible(false);swarm.activate(true);state=State.spread;StartCoroutine("transformSpread");}privatevoidturnAntiSwirl(){if(state==State.swirl){state=State.normal;turnVisible(true);swarm.activate(false);}}privatevoidturnAntiSlim(){if(state==State.slim){state=State.normal;StartCoroutine("transformAntiSlim");}}privatevoidturnAntiSpread(){if(state==State.spread){state=State.normal;StartCoroutine("transformAntiSpread");}}privatevoidresetTurn(){swarm.Collapse=0;swarm.Noise=0;}voidStart(){swarm=GetComponent<SwarmSystem>();swarm.activate(false);}voidFixedUpdate(){transform.Translate(deltaV);deltaV=0.6f*deltaV+0.4f*direcV;direcV=Vector3.zero;if(transform.position.y>verticalBound)transform.position=newVector3(transform.position.x,verticalBound,transform.position.z);if(transform.position.y<-verticalBound)transform.position=newVector3(transform.position.x,-verticalBound,transform.position.z);if(transform.position.x<-horizontalBound)transform.position=newVector3(-horizontalBound,transform.position.y,transform.position.z);if(transform.position.x>horizontalBound)transform.position=newVector3(horizontalBound,transform.position.y,transform.position.z);slitherPhase+=0.1f;foreach(Transformtinswarm.meshRender.bones){t.Translate(newVector3(0,Mathf.Sin(t.position.x-transform.position.x+slitherPhase)*0.002f,0),Space.World);}foreach(Rendererrinsolids){r.transform.Translate(newVector3(0,Mathf.Sin(r.transform.position.x-transform.position.x+slitherPhase+0.2f)*-0.008f,0),Space.World);}}voidUpdate(){if(hurtCooldown>0)hurtCooldown-=Time.deltaTime;// keyboard schemeif(Input.GetKey("w"))moveUp();if(Input.GetKey("a"))moveLeft();if(Input.GetKey("s"))moveDown();if(Input.GetKey("d"))moveRight();if(Input.GetKeyDown("i"))turnSwirl();if(Input.GetKeyUp("i"))turnAntiSwirl();if(Input.GetKeyDown("j"))turnSlim();if(Input.GetKeyUp("j"))turnAntiSlim();if(Input.GetKeyDown("k"))turnSpread();if(Input.GetKeyUp("k"))turnAntiSpread();// xbox schemeif(Input.GetAxis("JoystickY")<-0.5f)moveUp();if(Input.GetAxis("JoystickX")<-0.5f)moveLeft();if(Input.GetAxis("JoystickY")>0.5f)moveDown();if(Input.GetAxis("JoystickX")>0.5f)moveRight();if(Input.GetButtonDown("X"))turnSwirl();if(Input.GetButtonUp("X"))turnAntiSwirl();if(Input.GetButtonDown("A"))turnSlim();if(Input.GetButtonUp("A"))turnAntiSlim();if(Input.GetButtonDown("Y"))turnSpread();if(Input.GetButtonUp("Y"))turnAntiSpread();}voidOnDrawGizmosSelected(){Gizmos.color=Color.cyan;Gizmos.DrawWireCube(Vector3.zero,newVector3(2*horizontalBound,2*verticalBound,0));}privateIEnumeratormakeGoAway(Transformt){Vector3delta=Random.onUnitSphere*Random.Range(0.03f,0.1f);Vector3start=t.position;while((start-t.position).magnitude<3){t.Translate(delta);yieldreturnnewWaitForSeconds(0.01f);}Destroy(t.gameObject);}privatevoidgetHurt(){if(hurtCooldown<=0){healthBar.health--;hurtCooldown=3;for(inti=0;i<30;i++){Transformt=((GameObject)Instantiate(hurtPrefab,transform.position+swarm.collapseCenter,Quaternion.identity)).transform;StartCoroutine(makeGoAway(t));}}}voidOnTriggerEnter(Colliderother){EnemyColliderenemy=other.gameObject.GetComponent<EnemyCollider>();if(enemy!=null){switch(enemy.type){caseObstacleType.caveWalls:caseObstacleType.rockTop:caseObstacleType.rockBottom:caseObstacleType.rockJaws:getHurt();break;caseObstacleType.narrowPassage:if(state!=State.slim)getHurt();break;caseObstacleType.net:if(state!=State.spread)getHurt();break;}}}}