//circle collider from Super Starblasters public CGameObject CollisionCylinder(Type instanceType) { CGameObject collidedInstance = null; CGameObject otherInstance = null; //getting the object to collide with //looping through the object list for (int i = 0; i < CObjectManager.MAX_INSTANCES; i++) { //if the other object type is the one we're looking for if ((CObjectManager.Instance.pGameObjectList[i] != null) && Object.ReferenceEquals(instanceType, CObjectManager.Instance.pGameObjectList[i].GetType())) { //getting the reference otherInstance = CObjectManager.Instance.pGameObjectList[i]; if (hitCylinder.isColliding(otherInstance.hitCylinder)) { collidedInstance = otherInstance; } } } return(collidedInstance); }
public CGameObject CollisionRectangle(Rectangle rectangle, Type instanceType, bool notMe) { CGameObject collidedInstance = null; CGameObject otherInstance = null; //looping through the gameobjectlist for (int i = 0; i < CObjectManager.MAX_INSTANCES; i++) { //if the other object type is the one we're looking for if ((CObjectManager.Instance.pGameObjectList[i] != null) && (notMe && CObjectManager.Instance.pGameObjectList[i] != this) && Object.ReferenceEquals(instanceType, CObjectManager.Instance.pGameObjectList[i].GetType())) { //getting the reference otherInstance = CObjectManager.Instance.pGameObjectList[i]; //checking if the rectangles overlap if (rectangle.Intersects(otherInstance.rCollisionRectangle) || rectangle.Contains(otherInstance.rCollisionRectangle)) { collidedInstance = otherInstance; } } } return(collidedInstance); }
private Vector2 EnemyCollision(Type instanceType, double collisionRadius) { //CGameObject collidedInstance = null; CGameObject otherInstance = null; Vector2 collisionLocation = new Vector2(this.x, this.y); //getting the object to collide with //looping through the object list for (int i = 0; i < CObjectManager.MAX_INSTANCES; i++) { //if the other object type is the one we're looking for if ((CObjectManager.Instance.pGameObjectList[i] != null) && Object.ReferenceEquals(instanceType, CObjectManager.Instance.pGameObjectList[i].GetType())) { //getting the reference otherInstance = CObjectManager.Instance.pGameObjectList[i]; for (int e = 0; e <= 360; e++) { int pointx = (int)this.x + (int)distDirX((float)collisionRadius, (float)e); int pointy = (int)this.y + (int)distDirY((float)collisionRadius, (float)e); if (otherInstance.rCollisionRectangle.Contains(pointx, pointy)) { collisionLocation = new Vector2((float)pointx, (float)pointy); break; } } } } return(collisionLocation); }
public override void Update() { UpdateCollision(); //wall collision //CGameObject collision = CollisionRectangle(new Rectangle(rCollisionRectangle.X, rCollisionRectangle.Y + rCollisionRectangle.Height, rCollisionRectangle.Width, 1), typeof(CWall), true); CGameObject player = FindInstance(typeof(CPlayer)); //bool playerInSight = FreeSightline(new Vector2(player.x, player.y)); EnemyMovement(1, new Vector2(player.x, player.y)); //animation for the ghost floating anim... if (iAnimTimer <= 0) { bAnim = !bAnim; iAnimTimer = iAnimCoolDown; } else { iAnimTimer--; } x += fHorSpeed; y += fVerSpeed; }
public override void Update() { CGameObject playerCollision = CollisionCylinder(typeof(CPlayer)); if (playerCollision != null) { CAudioManager.Instance.PlaySound("menuselect1"); CObjectManager.Instance.DestroyInstance(iIndex); } }
private int PickDirection(Vector2 target) { int dir = 0; //returns dir between 0-3, dir*90 = movemnt direction int gridX = (int)x / 16; int gridY = (int)y / 16; bool[] canMoveInDir = new bool[4]; for (int i = 0; i <= 3; i++) { CGameObject col = CollisionRectangle(new Rectangle((int)x + (int)distDirX(16, degToRad(i * 90)), (int)y + (int)distDirY(16, degToRad(i * 90)), 16, 16), typeof(CWall), true); //check if grid tile ahead is free if (col == null && i != (iDir + 2) % 4) { //it is free, so calculate the distance from said tile canMoveInDir[i] = true; } /*else * { * if (col != null) * Debug.Print("did not move in dir " + i + ", col was not null"); * else if (i == (iDir + 2) % 4) * Debug.Print("did not move in dir " + i + ", it was backtracking"); * else * Debug.Print("lol what the f**k"); * }*/ } float min = 666666666; for (int i = 0; i <= 3; i++) { if (canMoveInDir[i]) { float dist = (float)(Math.Pow((target.X - (x + distDirX(16, degToRad(i * 90)))), 2) + Math.Pow((target.Y - (y + distDirY(16, degToRad(i * 90)))), 2)); if (dist < min) { min = dist; //Debug.Print("dir " + i + " dist " + dist); dir = i; } } } return(dir); }
//creates a new instance and returns a reference to that instance public CGameObject CreateInstance(Type instanceType, float x, float z, float y) { CGameObject returnObject = null; //int debugObjSlot = 0; //find the first empty slot in the array and put the object there for (int i = 0; i < MAX_INSTANCES; i++) { if (pGameObjectList[i] == null) { //making the object!!!! try { returnObject = pGameObjectList[i] = (CGameObject)Activator.CreateInstance(instanceType); } catch (Exception e) { try { CConsole.Instance.Print("Could not create entity type of " + instanceType.ToString() + "! " + e.Message); } catch (Exception a) { CConsole.Instance.Print("Entity type was null! " + a.Message); } } if (returnObject != null) { //this is for the function //returnObject = pGameObjectList[i]; //here we make sure the object spawns correctly returnObject.Spawn(x, z, y, i); //debugObjSlot = i; } break; } } if (returnObject == null) { CConsole.Instance.Print("Entity creation failed!"); } return(returnObject); }
//returns first instance of given type, pretty trash hack public CGameObject FindInstance(Type instanceType) { CGameObject instance = null; for (int i = 0; i < CObjectManager.MAX_INSTANCES; i++) { //if the other object type is the one we're looking for if ((CObjectManager.Instance.pGameObjectList[i] != null) && Object.ReferenceEquals(instanceType, CObjectManager.Instance.pGameObjectList[i].GetType())) { instance = CObjectManager.Instance.pGameObjectList[i]; break; } } return(instance); }
public override void Update() { UpdateCollision(); //floor collision CGameObject collision = CollisionRectangle(new Rectangle(rCollisionRectangle.X, rCollisionRectangle.Y + rCollisionRectangle.Height, rCollisionRectangle.Width, 1), typeof(CWall), true); //input KeyboardState keyboardState = Keyboard.GetState(); MovementKeyboard(keyboardState); //capping the player rotation to [0.0f, 360.0f] fAimDirection = fAimDirection % 360; //Debug.Print("player aim dir " + fAimDirection); fHorSpeed = (float)distDirX((float)iFSpeed, degToRad(fAimDirection)) + (float)distDirX((float)iSSpeed, degToRad(fAimDirection + 90.0f)); fVerSpeed = (float)distDirY((float)iFSpeed, degToRad(fAimDirection)) + (float)distDirY((float)iSSpeed, degToRad(fAimDirection + 90.0f)); //note! current collision model only supports recantular collisions, no pixel perfect shapes //collision always gets stuck, needs adjusting int collisionSafeZone = 4; //collisions to the left and right if ((CollisionRectangle(new Rectangle(rCollisionRectangle.X - collisionSafeZone, rCollisionRectangle.Y, collisionSafeZone, rCollisionRectangle.Height), typeof(CWall), true) != null && fHorSpeed < 0) || (CollisionRectangle(new Rectangle(rCollisionRectangle.X + rCollisionRectangle.Width, rCollisionRectangle.Y, collisionSafeZone, rCollisionRectangle.Height), typeof(CWall), true) != null && fHorSpeed > 0)) { fHorSpeed = 0; } //collisions above and below if ((CollisionRectangle(new Rectangle(rCollisionRectangle.X, rCollisionRectangle.Y - collisionSafeZone, rCollisionRectangle.Width, collisionSafeZone), typeof(CWall), true) != null && fVerSpeed < 0) || (CollisionRectangle(new Rectangle(rCollisionRectangle.X, rCollisionRectangle.Y + rCollisionRectangle.Height, rCollisionRectangle.Width, collisionSafeZone), typeof(CWall), true) != null && fVerSpeed > 0)) { fVerSpeed = 0; } x += fHorSpeed; y += fVerSpeed; }
//point collider public CGameObject PointCollider(int collX, int collY, Type instanceType) { CGameObject collidedInstance = null; CGameObject otherInstance = null; //looping through the gameobject list for (int i = 0; i < CObjectManager.MAX_INSTANCES; i++) { //if the other object type is the one we're looking for and exists if (CObjectManager.Instance.pGameObjectList[i] != null && Object.ReferenceEquals(instanceType, CObjectManager.Instance.pGameObjectList[i].GetType())) { otherInstance = CObjectManager.Instance.pGameObjectList[i]; //if the point is inside the collision rectangle if (otherInstance.rCollisionRectangle.Contains(collX, collY)) { collidedInstance = otherInstance; } } } return(collidedInstance); }
public override void Update() { UpdateCollision(); hitCylinder.SetPos(new Vector3(x, z, y)); //floor collision CGameObject natsaCollision = CollisionCylinder(typeof(CNatsa)); if (natsaCollision != null) { CGame.Instance.CollectNatsa(1); CObjectManager.Instance.DestroyInstance(natsaCollision.iIndex); } //input KeyboardState keyboardState = Keyboard.GetState(); GamePadState gamepadState = GamePad.GetState(PlayerIndex.One); if (!CSettings.Instance.bGamepadEnabled) { MovementKeyboard(keyboardState); } else { MovementGamepad(gamepadState); } PlayerPhysics(); PlayerCollision(fHeightBufferZone, 4.0f); if (fHInput2 != 0) { fAimDir -= (fHInput2 * 0.2f) / (float)Math.PI; } if (Math.Abs(fVInput2) >= 0.35) { fLightDistance -= (fVInput2 * 1.5f) / (float)Math.PI; if (fLightDistance > fLightMaxDistance) { fLightDistance = fLightMaxDistance; } else if (fLightDistance < fLightMinDistance) { fLightDistance = fLightMinDistance; } } //shoob if (CInputManager.ButtonDown(CSettings.Instance.gFire)) { if (iShotCooldown <= 0) { float xoffs = distDirX(5, fAimDir); float yoffs = distDirY(5, fAimDir); Vector3 bulletSpawnPoint = new Vector3(x + xoffs, z + 7, y + yoffs); CAudioManager.Instance.PlaySound("temp_gunshot"); CPlayerBullet bullet = (CPlayerBullet)CObjectManager.Instance.CreateInstance(typeof(CPlayerBullet), bulletSpawnPoint.X, bulletSpawnPoint.Y, bulletSpawnPoint.Z); bullet.SetProperties(fAimDir, 5); CParticleManager.Instance.CreateParticle("part_muzzleflash", bulletSpawnPoint); CParticleManager.Instance.CreateParticle("part_gunsmoke", bulletSpawnPoint); CParticleManager.Instance.CreateParticle("part_bulletcasing", bulletSpawnPoint, new Vector3(distDirX(0.3f, fAimDir - (float)(Math.PI / 2)), 0, distDirY(0.3f, fAimDir - (float)(Math.PI / 2)))); iShotCooldown = iShotCooldownMax; } testframe += 0.25f; } iShotCooldown--; //debug CLevel.Instance.UpdateActiveCell(x, y); //we don't want to update the animation in Render() because it'll animate while paused UpdateAnimation(); CGame.Instance.UpdatePlayer(new Vector2(x, y)); vLightTarget = new Vector3(x + distDirX(fLightDistance, fAimDir), z, y + distDirY(fLightDistance, fAimDir)); CRender.Instance.SetLightPosition(new Vector3(x, z + 10, y), vLightTarget); }
public void SetTarget(CGameObject targetObject) { oTarget = targetObject; }
public override void Update() { UpdateCollision(); //wall collision //CGameObject collision = CollisionRectangle(new Rectangle(rCollisionRectangle.X, rCollisionRectangle.Y + rCollisionRectangle.Height, rCollisionRectangle.Width, 1), typeof(CWall), true); CGameObject player = FindInstance(typeof(CPlayer)); bool playerInSight = FreeSightline(new Vector2(player.x, player.y)); if (timer <= 0) { if (playerInSight) { //fire gun if (iShotTimer >= iShotCooldown) { CEnemyBullet bullet = (CEnemyBullet)CObjectManager.Instance.CreateInstance(typeof(CEnemyBullet), this.x, this.y); bullet.SetAimdir(fAimDirection); CAudioManager.Instance.PlaySound("rapidfireshot.wav"); iShotTimer = 0; } else { iShotTimer++; } iFSpeed = 0; vTarget = new Vector2(player.x, player.y); //Debug.Print("set target to " + vTarget.X + " " + vTarget.Y); fAimDirection = -(float)PointDirection(this.x, this.y, vTarget.X, vTarget.Y); bChasing = true; } else { if (bChasing) { ChaseTarget(vTarget, 1); Rectangle rect = new Rectangle((int)vTarget.X - 2, (int)vTarget.Y - 2, 4, 4); if (rect.Contains((int)x, (int)y)) { iFSpeed = 0; bChasing = false; Debug.Print("stopped chasing"); } } } } else { timer--; } fHorSpeed = (float)distDirX((float)iFSpeed, fAimDirection); fVerSpeed = (float)distDirY((float)iFSpeed, fAimDirection); //note! current collision model only supports recantular collisions, no pixel perfect shapes //collision always gets stuck, needs adjusting //int collisionSafeZone = 4; //collisions to the left and right /*if ((CollisionRectangle(new Rectangle(rCollisionRectangle.X - collisionSafeZone, rCollisionRectangle.Y, collisionSafeZone, rCollisionRectangle.Height), typeof(CWall), true) != null && fHorSpeed < 0) || * (CollisionRectangle(new Rectangle(rCollisionRectangle.X + rCollisionRectangle.Width, rCollisionRectangle.Y, collisionSafeZone, rCollisionRectangle.Height), typeof(CWall), true) != null && fHorSpeed > 0)) * fHorSpeed = 0; * * //collisions above and below * if ((CollisionRectangle(new Rectangle(rCollisionRectangle.X, rCollisionRectangle.Y - collisionSafeZone, rCollisionRectangle.Width, collisionSafeZone), typeof(CWall), true) != null && fVerSpeed < 0) || * (CollisionRectangle(new Rectangle(rCollisionRectangle.X, rCollisionRectangle.Y + rCollisionRectangle.Height, rCollisionRectangle.Width, collisionSafeZone), typeof(CWall), true) != null && fVerSpeed > 0)) * fVerSpeed = 0;*/ /*CGameObject coll = CollisionCircle(typeof(CWall), 16); * * while (coll != null) * { * x += (float)distDirX((float)2, -(float)PointDirection(this.x, this.y, coll.x, coll.y)); * y += (float)distDirY((float)2, -(float)PointDirection(this.x, this.y, coll.x, coll.y)); * coll = CollisionCircle(typeof(CWall), 16); * }*/ x += fHorSpeed; y += fVerSpeed; }