GreatSpringGameJam/GreatSpringGameJam/WaterDrop.cs

85 lines
3.2 KiB
C#
Raw Normal View History

2021-03-08 02:37:40 +01:00
using System;
2021-03-09 20:13:26 +01:00
using System.Collections.Generic;
using Coroutine;
2021-03-08 02:37:40 +01:00
using Microsoft.Xna.Framework;
2021-03-17 22:57:20 +01:00
using Microsoft.Xna.Framework.Audio;
2021-03-08 02:37:40 +01:00
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extended.Tiled;
2021-03-17 22:57:20 +01:00
using MLEM.Startup;
2021-03-08 02:37:40 +01:00
using MLEM.Textures;
using MonoGame.Extended;
2021-03-14 02:04:57 +01:00
using RectangleF = MLEM.Misc.RectangleF;
2021-03-08 02:37:40 +01:00
namespace GreatSpringGameJam {
public class WaterDrop : Entity {
2021-03-17 22:57:20 +01:00
private static readonly SoundEffect PlantGrowSound = MlemGame.LoadContent<SoundEffect>("Sounds/PlantGrow");
2021-03-08 02:37:40 +01:00
private readonly bool growsPlants;
private Vector2 velocity;
private TimeSpan timeToLive;
2021-03-08 16:04:51 +01:00
private bool onGround;
2021-03-08 02:37:40 +01:00
public WaterDrop(Map map, Vector2 position, Vector2 velocity) : base(map, position) {
this.velocity = velocity;
this.timeToLive = TimeSpan.FromSeconds(0.5F);
this.growsPlants = Random.NextSingle() <= 0.25F;
}
public override void Update(GameTime time) {
base.Update(time);
this.Position += this.velocity;
2021-03-08 16:04:51 +01:00
this.Collide(() => new RectangleF(this.Position - new Vector2(1 / 16F), new Vector2(2 / 16F)), ref this.velocity, ref this.onGround);
if (this.onGround)
2021-03-08 02:37:40 +01:00
this.velocity = Vector2.Zero;
this.velocity.Y += 0.008F;
this.timeToLive -= time.ElapsedGameTime;
if (this.timeToLive <= TimeSpan.Zero) {
2021-03-09 20:13:26 +01:00
this.Map.RemoveEntity(this);
// growing plants
if (!this.growsPlants)
return;
var (x, y) = this.Position.ToPoint();
var tile = this.Map.GetTile("Ground", x, y);
if (tile.IsBlank)
return;
var tileset = this.Map.GetTileset(tile);
var tilesetTile = this.Map.GetTilesetTile(tile, tileset);
var grown = tilesetTile.Properties.GetInt("GrownVersion");
if (grown == 0)
return;
var grownTile = tileset.GetTilesetTile(grown);
this.Map.SetTile("Ground", x, y, tileset, grownTile);
2021-03-17 22:57:20 +01:00
PlantGrowSound.Play();
2021-03-09 20:13:26 +01:00
2021-03-14 02:04:57 +01:00
// vines
2021-03-09 20:13:26 +01:00
if (grownTile.Properties.GetBool("Vine")) {
IEnumerable<Wait> GrowVines() {
while (y-- > 0) {
yield return new Wait(0.2F);
if (this.Map.GetTile("Ground", x, y).GlobalIdentifier != 0)
break;
this.Map.SetTile("Ground", x, y, tileset, grownTile);
}
2021-03-08 02:37:40 +01:00
}
2021-03-09 20:13:26 +01:00
CoroutineHandler.Start(GrowVines());
2021-03-08 02:37:40 +01:00
}
2021-03-14 02:04:57 +01:00
// forget me not
2021-03-17 22:57:20 +01:00
if (grownTile.Properties.GetBool("ForgetMeNot")) {
2021-03-14 02:04:57 +01:00
this.Map.ForgetMeNotTime = TimeSpan.FromSeconds(10);
2021-03-17 22:57:20 +01:00
Map.ForgetMeNotPlatformSound.Play();
}
2021-03-08 02:37:40 +01:00
}
}
public override void Draw(GameTime time, SpriteBatch batch) {
base.Draw(time, batch);
var tex = StuffTexture[3, 0];
batch.Draw(tex, this.Position * this.Map.TileSize - tex.Size.ToVector2() / 2, Color.White);
}
}
}