Skip to content

Instantly share code, notes, and snippets.

@neipo13
Created December 12, 2018 21:08
Show Gist options
  • Save neipo13/1b7ed5ab83496d73958e51cc951d846a to your computer and use it in GitHub Desktop.
Save neipo13/1b7ed5ab83496d73958e51cc951d846a to your computer and use it in GitHub Desktop.
Spinning Circle Effect
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nez;
using Nez.Sprites;
using System.Collections.Generic;
namespace TemplateGame.Scenes
{
public class CircleEffectTestScene : Scene
{
Texture2D blackCircle;
Texture2D whiteCircle;
CameraShake camShake;
List<Entity> circles;
public override void initialize()
{
base.initialize();
clearColor = Color.Black;
addRenderer(new DefaultRenderer());
blackCircle = content.Load<Texture2D>("images/blackCircle");
whiteCircle = content.Load<Texture2D>("images/whiteCircle");
power = powerMax;
camShake = camera.addComponent<CameraShake>();
circles = new List<Entity>();
}
const float powerMax = 5f;
float power = 0f;
int dirChanger = 1;
public override void update()
{
base.update();
if (Nez.Input.leftMouseButtonDown && power > 0f)
{
var circle = AddCircle(power, Nez.Input.mousePosition, Nez.Input.mousePositionDelta.ToVector2());
circles.Add(circle);
camShake.shake(power);
power -= Time.deltaTime * 5f;
}
else if (Nez.Input.leftMouseButtonReleased)
{
power = powerMax;
}
if (Nez.Input.isKeyPressed(Microsoft.Xna.Framework.Input.Keys.R))
{
circles.ForEach(c => c.destroy());
circles.Clear();
}
}
private Entity AddCircle(float scale, Vector2 pos, Vector2 mouseDir)
{
var e = addEntity(new Entity()).setPosition(pos);
var blackSprite = e.addComponent(new Sprite(blackCircle));
blackSprite.renderLayer = 0;
var whiteSprite = e.addComponent(new Sprite(whiteCircle));
whiteSprite.renderLayer = 1;
e.scale = new Vector2(scale);
var rotator = e.addComponent<CircleRotator>();
if(mouseDir != Vector2.Zero)
{
var mover = e.addComponent<CircleMover>();
var moveDir = new Vector2(mouseDir.Y, mouseDir.X);
moveDir.Normalize();
moveDir *= scale * 3f;
mover.direction = moveDir;
if(dirChanger < 0)
{
mover.direction *= -1;
}
dirChanger *= -1;
}
return e;
}
}
public class CircleRotator : Component, IUpdatable
{
public float rotationSpeed = 0.3f;
public void update()
{
this.entity.rotation += Time.deltaTime * rotationSpeed;
}
}
public class CircleMover : Component, IUpdatable
{
public Vector2 direction = new Vector2();
public float speed = 10f;
public float drag = 4f;
public void update()
{
if(speed <= 0f) entity.removeComponent(this);
entity.position += direction * Time.deltaTime * speed;
speed = Mathf.lerp(speed, 0f, drag * Time.deltaTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment