public static void Create(ECS.Scene scene, ECS.ComponentList <Components.Transform> transforms, ECS.ComponentList <Components.Body> bodies, ECS.ComponentList <Components.Behavior> behaviors, ECS.ComponentList <Components.Avatar> avatars, Random random, int W, int H) { ushort pos; ECS.Entity e = scene.NewEntity(); pos = scene.AttachComponent <Components.Transform>(e, transforms); transforms.data[pos].position.X = (float)random.NextDouble() * W; transforms.data[pos].position.Y = (float)random.NextDouble() * H; pos = scene.AttachComponent <Components.Body>(e, bodies); bodies.data[pos].velocity.X = 0; bodies.data[pos].velocity.Y = 0; pos = scene.AttachComponent <Components.Behavior>(e, behaviors); behaviors.data[pos].isPlayerControlled = false; pos = scene.AttachComponent <Components.Avatar>(e, avatars); avatars.data[pos].animations = new Dictionary <string, Components.Animation>(); Components.Animation andando = new Components.Animation(scene, "bolinha", 32, 32, new int[] { 500, 300, 200, 100, 100, 100, 100, 100, 100, 100, 100, 200, 300 }, true); Components.Animation parada = new Components.Animation(scene, "bolinha", 32, 32, new int[] { 1 }, false); avatars.data[pos].animations.Add("parada", parada); avatars.data[pos].animations.Add("andando", andando); avatars.data[pos].currentAnimation = andando; }
public static void Create(ECS.Scene scene, ECS.ComponentList <Components.Transform> transforms, ECS.ComponentList <Components.Body> bodies, ECS.ComponentList <Components.Behavior> behaviors, ECS.ComponentList <Components.Avatar> avatars) { ushort pos; ECS.Entity e = scene.NewEntity(); pos = scene.AttachComponent <Components.Transform>(e, transforms); transforms.data[pos].position.X = 400; transforms.data[pos].position.Y = 400; pos = scene.AttachComponent <Components.Body>(e, bodies); bodies.data[pos].velocity.X = 0; bodies.data[pos].velocity.Y = 0; pos = scene.AttachComponent <Components.Behavior>(e, behaviors); behaviors.data[pos].isPlayerControlled = true; pos = scene.AttachComponent <Components.Avatar>(e, avatars); avatars.data[pos].animations = new Dictionary <string, Components.Animation>(); Components.Animation parado = new Components.Animation(scene, "vivo_idle", 32, 32, new int[] { 140, 140 }, true); Components.Animation andando = new Components.Animation(scene, "vivo_walk", 32, 32, new int[] { 140, 140 }, true); avatars.data[pos].animations.Add("parado", parado); avatars.data[pos].animations.Add("andando", andando); avatars.data[pos].currentAnimation = parado; }
public void Draw(GameTime gameTime, SpriteBatch spriteBatch) { for (ushort i = 0; i < avatars.size; i++) { ECS.Entity entity = avatars.metadata[i].entity; int transformIndex; if (entity.components[transformsPos] == -1) { continue; } else { transformIndex = entity.components[transformsPos]; } Components.Animation animation = avatars.data[i].currentAnimation; animation.Run(gameTime); spriteBatch.Draw(animation.spriteSheet, Vector2.Subtract(transforms.data[transformIndex].position, new Vector2(16, 16)), animation.Rect(), Color.White); } }
public void Update(GameTime gameTime) { int temperature = 2; for (ushort i = 0; i < behaviors.size; i++) { ECS.Entity entity = behaviors.metadata[i].entity; int bodyIndex; int avatarIndex; if (entity.components[bodiesPos] == -1) { continue; } else { bodyIndex = entity.components[bodiesPos]; } avatarIndex = entity.components[avatarsPos]; if (behaviors.data[i].isPlayerControlled) { GamePadState gamepad = GamePad.GetState(PlayerIndex.One); Vector2 thumbSticks = new Vector2(0, 0); if (gamepad.DPad.Up == ButtonState.Pressed || gamepad.DPad.Right == ButtonState.Pressed || gamepad.DPad.Down == ButtonState.Pressed || gamepad.DPad.Left == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.W) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.S) || Keyboard.GetState().IsKeyDown(Keys.A)) { if (gamepad.DPad.Up == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.W)) { thumbSticks.Y = 1; } else if (gamepad.DPad.Down == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.S)) { thumbSticks.Y = -1; } if (gamepad.DPad.Right == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.D)) { thumbSticks.X = 1; } else if (gamepad.DPad.Left == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.A)) { thumbSticks.X = -1; } } else { thumbSticks.X = gamepad.ThumbSticks.Left.X; thumbSticks.Y = gamepad.ThumbSticks.Left.Y; } float normSquared = thumbSticks.X * thumbSticks.X + thumbSticks.Y + thumbSticks.Y; if (normSquared > 1) { thumbSticks.X /= (float)Math.Sqrt(normSquared); thumbSticks.Y /= (float)Math.Sqrt(normSquared); } float epsilon = 0.1f; float speed = 100; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) || gamepad.Triggers.Right > epsilon) { speed *= 2f; } bodies.data[bodyIndex].velocity.X = thumbSticks.X * speed; bodies.data[bodyIndex].velocity.Y = thumbSticks.Y * speed * -1; if (avatarIndex != -1) { normSquared = bodies.data[bodyIndex].velocity.X * bodies.data[bodyIndex].velocity.X + bodies.data[bodyIndex].velocity.Y * bodies.data[bodyIndex].velocity.Y; if (normSquared > epsilon) { Components.Animation anim = avatars.data[avatarIndex].animations["andando"]; if (avatars.data[avatarIndex].currentAnimation != anim) { avatars.data[avatarIndex].currentAnimation = anim; anim.currentFrame = 0; } } else { Components.Animation anim = avatars.data[avatarIndex].animations["parado"]; if (avatars.data[avatarIndex].currentAnimation != anim) { avatars.data[avatarIndex].currentAnimation = anim; anim.currentFrame = 0; } } } } else { bodies.data[bodyIndex].velocity.X += temperature * (float)(random.NextDouble() - 0.5); bodies.data[bodyIndex].velocity.Y += temperature * (float)(random.NextDouble() - 0.5); } } }