public Body(Layer layer, Vector2 position) : base(layer) { ICollider collider; if (_r.Next(2) == 0) { collider = new RectangleCollider( position, new Vector2(_r.Next(32, 64), _r.Next(32, 64)) / 2f ); } else { collider = new CircleCollider( position, _r.Next(16, 48) / 2f ); } AddComponent(new CPosition(position)); var phy = new CPhysics(1); phy.Collider = collider; AddComponent(phy); }
public override void CreateScene() { Scene = SceneMgr.CreateScene("Entity Component System"); var layer = Scene.CreateLayer("Entity Component System"); for (var i = 0; i < 20; i += 1) { var bot = Entity.CreateFromTemplate(layer, "Bot"); var position = bot.GetComponent <PositionComponent>(); position.Position = new Vector2(Random.Next(100, 700), Random.Next(100, 500)); } new Player(layer, new Vector2(400, 300)); new ECSDemo(layer); }
/// <summary> /// 将拼图碎片在给定范围内随机分布。 /// </summary> /// <param name="rect">碎片的分布矩形。</param> /// <param name="rotatable">是否允许旋转拼图碎片。</param> public void SpreadPieces(RectangleF rect, bool rotatable) { foreach (JigsawPiece p in this) { if (rotatable) { p.Rotate = RandomExt.Next(4) * 90; } RectangleF bounds = p.Bounds; float x = RandomExt.NextSingle() * (rect.Width - bounds.Width) + rect.X - bounds.X + p.Offset.X; float y = RandomExt.NextSingle() * (rect.Height - bounds.Height) + rect.Y - bounds.Y + p.Offset.Y; p.Offset = new Vector2(x, y); } }
public override void CreateScene() { Scene = SceneMgr.CreateScene("Entity Component"); var layer = Scene.CreateLayer("Entity Component"); for (var i = 0; i < 20; i += 1) { var bot = Entity.CreateFromTemplate(layer, "Bot"); var position = bot.GetComponent <PositionComponent>(); position.Position = new Vector2(Random.Next(100, 700), Random.Next(100, 500)); } var player = new Player(layer, new Vector2(400, 300)); // Now player will be drawn below all the bots, even though he was created last. // Reaordering puts him on the top of entity list. layer.ReorderEntityToTop(player); //layer.ReorderEntityToBottom(player); // Will have no effect. //layer.ReorderEntity(player, 2); // Player will be updated and drawn only after two entities above him. new ECDemo(layer); }
public Scene(Sprite map) { var texture = map.Frames[0].Texture; var colorData = new Color[texture.Width * texture.Height]; texture.GetData(colorData); // Map loader won't support sprites on texture atlas!!! TileMap = new int[texture.Width, texture.Height]; for (var y = 0; y < texture.Height; y += 1) { for (var x = 0; x < texture.Width; x += 1) { TileMap[x, y] = 1; var color = colorData[x + y * texture.Width]; if (CheckValue(_wall, color)) { new Solid(new Vector2(x, y) * CellSize, Vector2.One * CellSize, SpritesDefault.Block); } if (CheckValue(_tree, color)) { new Solid(new Vector2(x, y) * CellSize, Vector2.One * CellSize, SpritesDefault.Tree); } if (CheckValue(_player, color)) { new Player(new Vector2(x, y) * CellSize + Vector2.One * CellSize / 2); } if (CheckValue(_enemy, color)) { new Enemy(new Vector2(x, y) * CellSize + Vector2.One * CellSize / 2, CreatePath(x, y, colorData, texture.Width)); } if (CheckValue(_sky, color)) { TileMap[x, y] = 0; } if (CheckValue(_paintedTile, color)) { TileMap[x, y] = 2; } if (CheckValue(_skyBorder, color)) { new Solid(new Vector2(x, y) * CellSize, Vector2.One * CellSize); TileMap[x, y] = 0; } if (CheckValue(_npc, color)) { new NPC(new Vector2(x, y) * CellSize + Vector2.One * CellSize / 2, color.B); } if (CheckValue(_laser, color)) { new Laser( new Vector2(x, y) * CellSize + Vector2.One * CellSize / 2, CreateLaserPath(x, y, colorData, texture.Width) * CellSize + Vector2.One * CellSize / 2 ); } if (CheckValue(_checkpoint, color)) { new Checkpoint(new Vector2(x, y) * CellSize + Vector2.One * CellSize / 2); } if (CheckValue(_dialogue, color)) { new DialogueTrigger(new Vector2(x, y) * CellSize + Vector2.One * CellSize / 2, color.B); } if (CheckValue(_coin, color)) { new Coin(new Vector2(x, y) * CellSize); } } } var r = new RandomExt(42312342); for (var i = 0; i < 50; i += 1) { new Cloud( new Vector2(r.Next(-400, texture.Width * CellSize), r.Next(-400, texture.Height * CellSize)), r.Next(3), (float)r.NextDouble(0.3, 0.7) ); } }