// Public functions. public Player(Scene scene) { textureInfo = new TextureInfo(new Texture2D("/Application/assets/player-spritemap.png", false), new Vector2i(13, 1)); sprite = new SpriteTile(textureInfo); sprite.Position = new Vector2(AppMain.ScreenWidth * .2f, AppMain.ScreenHeight * .5f); sprite.Quad.S = textureInfo.TileSizeInPixelsf; sprite.CenterSprite(TRS.Local.Center); // Player variables isAlive = true; moveState = MoveStatus.Disabled; rotateAngle = .0f; // Attach update fucntion to scheduler sprite.Schedule(Update); // Create animation function sprite.ScheduleInterval((dt) => { if (IsAlive) { int tileIndex = sprite.TileIndex1D < 8 ? sprite.TileIndex1D + 1 : 1; sprite.TileIndex1D = tileIndex; } }, 0.2f); sprite.ScheduleInterval((dt) => { if (!IsAlive) { int tileIndex = sprite.TileIndex1D < 12 ? sprite.TileIndex1D + 1 : 12; sprite.TileIndex1D = tileIndex; } }, 0.16f); // Add to the current scene. scene.AddChild(sprite); }
public static Scene MakeManySpritesScene() { var tex1 = new TextureInfo(new Texture2D("/Application/Sample/GameEngine2D/FeatureCatalog/data/slime_green_frames.png", false) , new Vector2i(4, 4)); var scene = new Scene() { Name = "many SpriteUV scene" }; Bounds2 bounds = scene.Camera.CalcBounds(); Vector2i num_cells = new Vector2i(8, 8); Vector2 cell_size = bounds.Size / num_cells.Vector2(); Math.RandGenerator rgen = new Math.RandGenerator(); System.Random random = new System.Random(); for (int y = 0; y < num_cells.Y; ++y) { for (int x = 0; x < num_cells.X; ++x) { Vector2 uv = new Vector2((float)x, (float)y) / num_cells.Vector2(); var sprite = new SpriteTile() { TextureInfo = tex1 , Color = Math.SetAlpha(new Vector4(0.5f) + rgen.NextVector4(Math._0000, Math._1111) * 0.5f, 0.75f) //, Color = Math.SetAlpha( new Vector4(0.5f), 1f/*0.75f*/ ) , BlendMode = BlendMode.None , TileIndex2D = new Vector2i(random.Next(0, 4), random.Next(0, 4)) }; Vector2 p = bounds.Min + bounds.Size * uv; // init position/size sprite.Position = p; sprite.Quad.S = cell_size; // note: we don't want to touch the Node.Scale here sprite.Pivot = sprite.Quad.S * 0.5f; sprite.Schedule((dt) => { sprite.Rotation = sprite.Rotation.Rotate(Math.Deg2Rad(1.0f)); sprite.Rotation = sprite.Rotation.Normalize(); if (Common.FrameCount % 8 == 0) { sprite.TileIndex1D = (sprite.TileIndex1D + 1) % 16; } }); scene.AddChild(sprite); } } scene.RegisterDisposeOnExit((System.IDisposable)tex1); return(scene); }
public static Scene MakeManySpritesInSpriteListScene() { var tex1 = new TextureInfo(new Texture2D("/Application/Sample/GameEngine2D/FeatureCatalog/data/water_puddle.png", false) , new Vector2i(6, 1)); var scene = new Scene() { Name = "many SpriteTile in SpriteList scene" }; Bounds2 bounds = scene.Camera.CalcBounds(); Vector2i num_cells = new Vector2i(16, 8); Vector2 cell_size = bounds.Size / num_cells.Vector2(); Math.RandGenerator rgen = new Math.RandGenerator(); System.Random random = new System.Random(); SpriteList sprite_list = new SpriteList(tex1) { BlendMode = BlendMode.None }; sprite_list.EnableLocalTransform = true; scene.AddChild(sprite_list); for (int y = 0; y < num_cells.Y; ++y) { for (int x = 0; x < num_cells.X; ++x) { Vector2 uv = new Vector2((float)x, (float)y) / num_cells.Vector2(); var sprite = new SpriteTile() { // all those properties (TextureInfo, Color, BlendMode) will be ignored // if sprite is used in SpriteList TextureInfo = tex1 , Color = Math.SetAlpha(new Vector4(0.8f) + rgen.NextVector4(Math._0000, Math._1111) * 0.2f, 0.75f) , BlendMode = BlendMode.Normal // TileIndex2D can still be set per sprite , TileIndex2D = new Vector2i(random.Next(0, 6), 0) }; Vector2 p = bounds.Min + bounds.Size * uv; // init position/size if (sprite_list.EnableLocalTransform) { // use .Position as we did in MakeManySpritesScene sprite.Position = p; } else { // .Position will be ignored, so use the geometry directly sprite.Quad.T = p; } sprite.Quad.S = cell_size; // note: we don't want to touch the Node.Scale here sprite.Pivot = sprite.Quad.S * 0.5f; sprite.Schedule((dt) => { sprite.Rotation = sprite.Rotation.Rotate(Math.Deg2Rad(1.0f)); sprite.Rotation = sprite.Rotation.Normalize(); // if you turn EnableLocalTransform off for performances, you can // still move the sprite freely by changing its geometry directly: // sprite.Quad.R = sprite.Quad.R.Rotate( Math.Deg2Rad( 1.0f ) ); // sprite.Quad.R = sprite.Quad.R.Normalize(); // if (Common.FrameCount % 7 == 0) { sprite.TileIndex1D = (sprite.TileIndex1D + 1) % 6; } }); sprite_list.AddChild(sprite); } } scene.RegisterDisposeOnExit((System.IDisposable)tex1); return(scene); }
public static Scene MakeForestScene() { // System.Console.WriteLine( "MakeForestScene" ); var scene = new Scene() { Name = "Forest" }; TextureInfo ObjectsMap = new TextureInfo(new Texture2D("/Application/Sample/GameEngine2D/FeatureCatalog/data/PlanetCute/Objects.png", false), new Vector2i(7, 3)); Vector2i[] tree_indexes = new Vector2i[3]; tree_indexes[0] = PlanetCute.TreeShort; tree_indexes[1] = PlanetCute.TreeTall; tree_indexes[2] = PlanetCute.TreeUgly; Math.RandGenerator rgen = new Math.RandGenerator(); Bounds2 bounds = scene.Camera2D.CalcBounds(); Vector2i numcells = new Vector2i(7, 4); Vector2 cellsize = bounds.Size / numcells.Vector2(); System.Random rnd1 = new System.Random(); SpriteList sprite_list = new SpriteList(ObjectsMap); // we are going to put a tree at a random location inside each cell of a regular grid for (int x = 0; x < numcells.X; ++x) { // generate rows of tree top to bottom, for draw order for (int y = numcells.Y - 1; y >= 0; --y) { Vector2 cellindexf = new Vector2((float)x, (float)y); var sprite = new SpriteTile(); // sprite.TextureInfo = tree_tex[rnd1.Next(3)]; sprite.TextureInfo = ObjectsMap; sprite.TileIndex2D = tree_indexes[rnd1.Next(3)]; // bounds for one cell Bounds2 gen_bounds = new Bounds2(bounds.Min + cellindexf * cellsize, bounds.Min + cellindexf * cellsize + cellsize); // scale gen_bounds to countrols irregularity gen_bounds = gen_bounds.Scale(new Vector2(0.6f), gen_bounds.Center); // pick up a random point in that cell sprite.Position = gen_bounds.Min + gen_bounds.Size * rgen.NextVector2(Math._00, Math._11); // make the size of the tree match the size of cells, preserve texture ratio Vector2 aspect = sprite.CalcSizeInPixels() / sprite.CalcSizeInPixels().X; sprite.Quad.S = cellsize.X * aspect; // make the sprite bottom center point be the new pivot (for Skew) sprite.Quad.Centering(TRS.Local.BottomCenter); // make the trees move in the wind sprite.Schedule((dt) => { int hc = sprite.GetHashCode(); System.Random rnd2 = new System.Random(hc); sprite.Skew = new Vector2(Math.Deg2Rad(1.0f) * FMath.Sin((float)Director.Instance.DirectorTime * 1.0f * rnd2.Next(4096) / 4096.0f), Math.Deg2Rad(2.0f) * FMath.Sin((float)Director.Instance.DirectorTime * 3.0f * rnd2.Next(4096) / 4096.0f)); }); sprite_list.AddChild(sprite); } } scene.Camera2D.Center += new Vector2(0.0f, 2.0f); scene.AddChild(sprite_list); scene.RegisterDisposeOnExit((System.IDisposable)ObjectsMap); return(scene); }
public BasicEnemy(Vector2 pos, TextureInfo tex) { Position = pos; sprite = new SpriteTile(); sprite.TextureInfo = tex; sprite.Position = pos; sprite.TileIndex2D = new Vector2i(0, 0); sprite.CenterSprite(new Vector2(0.4f, 0.5f)); sprite.Scale = new Vector2(2.0f, 2.0f); //set up the random vector randomMovement = Support.rand.NextVector2(-0.01f, 0.01f); //update the animation frames sprite.Schedule((dt) => { if (FrameCount % 2 == 0) { //if attacking,use all animation frames if (attacking) { //deal damage to the player if (FrameCount % damageDelay == 0) { Player.Instance.Health -= damage; } animationFrame = (animationFrame + 1) % 25; } else { //if not,then use first three animation frames animationFrame = (animationFrame + 1) % 4; } //assign the correct tileindex sprite.TileIndex1D = animationFrame; //if close to the player,then follow,otherwise move randomly if (Player.Instance.Position.Distance(sprite.Position) < 6.0f && Collisions.checkLineOfSight(this, Player.Instance)) { isMovingRandomly = false; step = (Player.Instance.Position - sprite.Position).Normalize() / 15.0f; //check if should be attacking if (Collisions.checkCollisionBetweenEntities(this, Player.Instance)) { //check if is not attacking already if (attacking == false) { attacking = true; //manualy skip the frames animationFrame = 4; } } else { //make sure that attacking is set to false attacking = false; } } else { //player is not within range, move randomly isMovingRandomly = true; step = randomMovement; } } //advance the position by the given Vector2 step sprite.Position += step; //only move when not attacking if (!attacking) { //collision detection on the X axis //create a vector 2 containing a proposed change to the position Vector2 proposedChange = new Vector2(step.X, 0.0f) * speedModifier; //temporary game entity to contain the entity the enemy might have collided with GameEntity tempEntity; //check wall collisions and then collisions with other enemies if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity)) { //no collision, so we can change the position Position += proposedChange / speedModifier; } else if (isMovingRandomly) { //collided with something,but if the enemy should be moving randomly, then let it move randomMovement.X = -randomMovement.X; } //collision detection on the Y axis proposedChange = new Vector2(0.0f, step.Y) * speedModifier; if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity)) { Position += proposedChange / speedModifier; } else if (isMovingRandomly) { randomMovement.Y = -randomMovement.Y; } //position changed, so we need to remove the entity from the QuadTree and add again //this is because the entity might be in the wrong place in the QuadTree and removing and re-adding is the only way to fix that Game.Instance.quadTree.removeEntity(this); Game.Instance.quadTree.insert(this); } //rotate the sprite to face the direction of walking var angleInRadians = -FMath.Atan2(step.X, step.Y); sprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); //correct for the fact that the sprite is rotated in the texture file sprite.Rotation = sprite.Rotation.Rotate(90.0f); }, -1); //create a shadow texture - temporarily disabled //SpriteUV shadow = new SpriteUV(new TextureInfo(Bullet.fireTexture)); //shadow.CenterSprite(new Vector2(0.5f,0.5f)); //shadow.Color = Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.SetAlpha(Colors.Black,0.5f); //calculate the bounds for the entire sprite bounds = new Bounds2(); sprite.GetlContentLocalBounds(ref bounds); bounds = new Bounds2(bounds.Min * 0.5f, bounds.Max * 0.5f); }