Skip to content

Commit

Permalink
Merge pull request #39 from evil-moustachio/popups
Browse files Browse the repository at this point in the history
Popups
  • Loading branch information
MaxWinsemius committed Jul 1, 2015
2 parents c04e897 + 02d4c5f commit 48462bc
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Entities/Bin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public virtual void AddGarbage(Garbage garbage)
if (_category == garbage.Category) {
// Positive
Model.GameRules.points += garbage.Points;
((Stage)_parentView).NewPlayerFeedback("+" + garbage.Points, Color.Green, _position, 75f, 30f);
((Stage)_view).NewPlayerFeedback("+" + garbage.Points, Color.Green, _position, 75f, 30f);
_game.soundEffect = new SoundHandler("Correct", Model.Settings.SoundEffectVolume);
_game.soundEffect.Play();
} else {
// Negative
Model.GameRules.points -= garbage.Points / 2;
((Stage)_parentView).NewPlayerFeedback("-" + (garbage.Points / 2), Color.Red, _position, 75f, 30f);
((Stage)_view).NewPlayerFeedback("-" + (garbage.Points / 2), Color.Red, _position, 75f, 30f);
_game.soundEffect = new SoundHandler("Wrong", Model.Settings.SoundEffectVolume);
_game.soundEffect.Play();
}
Expand Down
16 changes: 8 additions & 8 deletions src/Entities/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Monster(Texture2D texture, Game1 game, View view, Vector2 speed, float he
_type = type;

_nextAttack = Model.Time.CurrentGameTick;
_healthBar = new Healthbar (ContentHandler.GetTexture("HealthBarEntity"), _position, new Vector2(0,-25), _game, _parentView, _health);
_healthBar = new Healthbar (ContentHandler.GetTexture("HealthBarEntity"), _position, new Vector2(0,-25), _game, _view, _health);
}

private void Spawn()
Expand Down Expand Up @@ -88,7 +88,7 @@ private void Spawn()
}

futureHitBox = new Rectangle(x, y, HitBox.Height + 100 , HitBox.Width + 100);
spawned = ((Stage)_parentView).NotColliding(this, futureHitBox, _minCoords, _maxCoords);
spawned = ((Stage)_view).NotColliding(this, futureHitBox, _minCoords, _maxCoords);
position.X = x;
position.Y = y;
}
Expand Down Expand Up @@ -126,11 +126,11 @@ private void Move()
{
// Creates the next Hitbox with an updated position.
Vector2 oldPosition = _position;
Vector2 nextPosition = MoveToTarget(((Stage)_parentView).RatBase);
Vector2 nextPosition = MoveToTarget(((Stage)_view).RatBase);
Rectangle nextHitBox = new Rectangle ((int)nextPosition.X, (int)nextPosition.Y, HitBox.Width, HitBox.Height);

// Check if next position will cause a collision
if (((Stage)_parentView).NotColliding (this, nextHitBox, _minCoords, _maxCoords))
if (((Stage)_view).NotColliding (this, nextHitBox, _minCoords, _maxCoords))
{
_position = nextPosition;
}
Expand All @@ -142,11 +142,11 @@ private void Move()
Rectangle nextYHitbox = new Rectangle((int)_position.X, (int)nextPosition.Y, HitBox.Width, HitBox.Height);

// Updates the position if the move is allowed.
if (((Stage)_parentView).NotColliding(this, nextXHitbox, _minCoords, _maxCoords))
if (((Stage)_view).NotColliding(this, nextXHitbox, _minCoords, _maxCoords))
{
_position.X = nextPosition.X;
}
if (((Stage)_parentView).NotColliding(this, nextYHitbox, _minCoords, _maxCoords))
if (((Stage)_view).NotColliding(this, nextYHitbox, _minCoords, _maxCoords))
{
_position.Y = nextPosition.Y;
}
Expand Down Expand Up @@ -174,7 +174,7 @@ private void Attack()
{
if (Model.Time.CurrentGameTick >= _nextAttack)
{
if (((Stage)_parentView).AttackHandler(this, _damage, AttackBox))
if (((Stage)_view).AttackHandler(this, _damage, AttackBox))
{
_nextAttack = Model.Time.CurrentGameTick + _atkspd;
_game.soundEffect = new SoundHandler("MonsterHitsRat", Model.Settings.SoundEffectVolume);
Expand Down Expand Up @@ -202,7 +202,7 @@ public override void KillEntity()
{
_game.soundEffect = new SoundHandler("MonsterDie", Model.Settings.SoundEffectVolume);
_game.soundEffect.Play();
((Stage)_parentView).MonsterToGarbage(this, _texture, _flip);
((Stage)_view).MonsterToGarbage(this, _texture, _flip);
}

public override void Update()
Expand Down
10 changes: 5 additions & 5 deletions src/Entities/Rat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Rat(Texture2D texture, Vector2 position, Game1 game, View view, Vector2 s
/// </summary>
private void Move()
{
Stage view = (Stage)_parentView;
Stage view = (Stage)_view;

//up
if (KeyHandler.IsKeyDown(Model.Settings.Key.Up) && view.NotColliding(this, MakeFutureRectangle(Directions.Up), _minCoords, _maxCoords))
Expand Down Expand Up @@ -185,15 +185,15 @@ private void Attack()
else
StartSingleMotion(0);

((Stage)_parentView).AttackHandler(this, _damage, AttackBox);
((Stage)_view).AttackHandler(this, _damage, AttackBox);
}
}

private void PickUp()
{
if (KeyHandler.checkNewKeyPressed(Model.Settings.Key.PickUp))
{
_inventory = ((Stage)_parentView).GarbageHandler(_inventory);
_inventory = ((Stage)_view).GarbageHandler(_inventory);
}
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public override void KillEntity()
_game.soundEffect = new SoundHandler("DeathSpiral", Model.Settings.SoundEffectVolume);
_game.soundEffect.Play();

((Stage)_parentView).NewPlayerFeedback("Fatality", Color.Red, new Vector2(_position.X - 30, _position.Y), 30f, 100f);
((Stage)_view).NewPlayerFeedback("Fatality", Color.Red, new Vector2(_position.X - 30, _position.Y), 30f, 100f);
_alive = false;
_gameOverTick = Model.Time.CurrentGameTick + (Model.Time.OneSecondOfTicks * 2);
}
Expand Down Expand Up @@ -259,7 +259,7 @@ public override void Update()
// Show GameOver Screen at right time
if (Model.Time.CurrentGameTick >= _gameOverTick)
{
((Stage)_parentView).GameOver();
((Stage)_view).GameOver();
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/GameObjects/AtlasObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class AtlasObject : GameObject
protected Texture2D _texture; //TODO: Change back to private later.
private Rectangle _hitBox;
private bool _animates;
private float _frameHeight, _frameWidth, _columns;
private float _frameHeight, _frameWidth;
private int _columns, _rows;
private long _nextFrameTick;
private float _ticksPerFrame;
private float _scale = 1.0f;
Expand Down Expand Up @@ -57,10 +58,11 @@ public override float LowestY
/// <param name="animates">If set to <c>true</c> animates.</param>
public AtlasObject(Texture2D texture, Vector2 position, Game1 game, View view, Color color, int rows, int columns, int totalFrames, bool animates) : base (position, game, view, color)
{
_texture = texture;
_frameHeight = _texture.Height / rows;
_frameWidth = _texture.Width / columns;
_columns = columns;
_texture = texture;
_columns = columns;
_rows = rows;
_frameWidth = _texture.Width / _columns;
_frameHeight = _texture.Height / _rows;

_animates = animates;

Expand Down Expand Up @@ -159,7 +161,7 @@ public override void Update()
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_texture, _position, new Rectangle((int)_sourceRectanglePosition.X, (int)_sourceRectanglePosition.Y,
(int)_sourceRectangleDimensions.X, (int)_sourceRectangleDimensions.Y), _color, _rotation, _origin, _scale, SpriteEffects.None, 0f);
(int)_sourceRectangleDimensions.X, (int)_sourceRectangleDimensions.Y), Color, _rotation, _origin, _scale, SpriteEffects.None, 0f);
}

private void updateHitbox()
Expand All @@ -170,5 +172,10 @@ private void updateHitbox()
(int)_sourceRectangleDimensions.X,
(int)_sourceRectangleDimensions.Y);
}

public Vector2 getSize()
{
return UtilHandler.getSize (_texture, (int)_columns, (int)_rows);
}
}
}
12 changes: 6 additions & 6 deletions src/GameObjects/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public abstract class GameObject
{
protected Vector2 _position;
protected Game1 _game;
protected View _parentView;
protected Color _color;
protected View _view;
public Color Color;

public Vector2 Position
{
Expand Down Expand Up @@ -44,15 +44,15 @@ public GameObject(Vector2 position, Game1 game, View view, Color color)
{
_position = position;
_game = game;
_parentView = view;
_color = color;
_view = view;
Color = color;
}

public GameObject(Game1 game, View view, Color color)
{
_game = game;
_parentView = view;
_color = color;
_view = view;
Color = color;
}

public virtual void Update() { }
Expand Down
35 changes: 34 additions & 1 deletion src/Handlers/UtilHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ public static Vector2 getSize(StringBuilder s, SpriteFont f)
return f.MeasureString (s);
}

public static Vector2 getSize(List<GameObject> g)
{
float maxX = 0f, maxY = 0f;
foreach (GameObject gameObject in g) {
if (gameObject is Text) {
float x = ((Text)gameObject).getSize ().X + gameObject.Position.X, y = ((Text)gameObject).getSize ().Y + gameObject.Position.Y;
if (x > maxX)
maxX = x;
if (y > maxY)
maxY = y;
} else if (gameObject is AtlasObject) {
float x = ((AtlasObject)gameObject).getSize ().X + gameObject.Position.X, y = ((AtlasObject)gameObject).getSize ().Y + gameObject.Position.Y;
if (x > maxX)
maxX = x;
if (y > maxY)
maxY = y;
} else if (gameObject is Frame) {
float x = ((Frame)gameObject).getSize ().X + gameObject.Position.X, y = ((Frame)gameObject).getSize ().Y + gameObject.Position.Y;
if (x > maxX)
maxX = x;
if (y > maxY)
maxY = y;
}
}

return new Vector2 (maxX, maxY);
}

public static Vector2 getCenter(Texture2D t, int columns, int rows)
{
return getSize(t, columns, rows) / 2;
Expand All @@ -62,7 +90,12 @@ public static Vector2 getCenter(string s, SpriteFont f)

public static Vector2 getCenter(StringBuilder s, SpriteFont f)
{
return getSize(s, f);
return getSize(s, f) / 2;
}

public static Vector2 getCenter(List<GameObject> g)
{
return getSize (g) / 2;
}

public static Vector2 getCenteredPosition(Texture2D t, Vector2 p, int columns, int rows)
Expand Down
15 changes: 14 additions & 1 deletion src/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public static class Model
{
// Counter used to show difference in update cycles in Console.WriteLine ()'s.
public static int counter = 0;

public static void Init()
{
Layout.Init ();
}

/// <summary>
/// Updates the Model.
/// </summary>
Expand Down Expand Up @@ -71,6 +77,13 @@ public static class Font
public static string ExtraExtraLarge = "Aero Matics Display-48";
}

public static Color OrangeColor { get; private set; }

public static void Init()
{
OrangeColor = new Color (251, 176, 59);
}

}

/// <summary>
Expand Down Expand Up @@ -127,7 +140,7 @@ public enum Category { Plastic, Paper, Chemical, Green, Other }
/// </summary>
public static class Settings
{
public static float MusicVolume = 0.3f;
public static float MusicVolume = 0.0f;
public static float SoundEffectVolume = 1f;

/// <summary>
Expand Down
54 changes: 52 additions & 2 deletions src/UIElements/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,64 @@ namespace Ratcycle
{
public class Frame : GameObject
{
List<GameObject> _gameObjects = new List<GameObject>();
private List<GameObject> _gameObjects = new List<GameObject>();
private List<Vector2> _gameObjectPositions = new List<Vector2> ();
protected Vector2 _size;

public Vector2 Size { get { return _size; } }

public Frame (Vector2 position, Game1 game, View view, List<GameObject> gameObjects) : base(position, game, view, Color.White)
{
_size = UtilHandler.getSize (gameObjects);
for (int i = gameObjects.Count - 1; i >= 0; i--) {
gameObjects [i].Position += _position;
_gameObjectPositions.Add (gameObjects [i].Position);
_gameObjects.Add (gameObjects [i]);
}

setChildPositions ();
}

protected void setChildPositions ()
{

for (int i = _gameObjects.Count - 1; i >= 0; i--) {
_gameObjects [i].Position = _gameObjectPositions [i] + _position;
}
}

public virtual void Resize()
{
_size = UtilHandler.getSize (_gameObjects);
}

public void AddGameObject(GameObject g)
{
_gameObjects.Add (g);
}

public void RemoveGameObject (GameObject g)
{
_gameObjects.Remove(g);
}

public Vector2 getSize()
{
return UtilHandler.getSize (_gameObjects);
}

public Vector2 getCenter()
{
return UtilHandler.getCenter (_gameObjects);
}

protected void setCenter(bool BasedOnSize)
{
Vector2 centerScreen = new Vector2 (_game.GraphicsDevice.Viewport.Width / 2, _game.GraphicsDevice.Viewport.Height / 2);
if (BasedOnSize) {
_position = centerScreen - new Vector2 (_size.X / 2, _size.Y / 2);
} else {
_position = centerScreen - getCenter ();
}
}

public override void Update ()
Expand Down
4 changes: 2 additions & 2 deletions src/UIElements/HitOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public override void Update()
_alpha = newAlpha < _alpha ? newAlpha : _alpha;
timer++;
if (_alpha < 0.05f)
((StageHUD)_parentView).removeItemFromGameObjects (this);
((StageHUD)_view).removeItemFromGameObjects (this);
}

public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw (_texture, _position, _color * _alpha);
spriteBatch.Draw (_texture, _position, Color * _alpha);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/UIElements/PlayerFeedback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public void Move()
public void Fade()
{
var updatesLeft = _duration - _counter;
Vector4 color = _color.ToVector4();
Vector4 color = Color.ToVector4();
color.X -= (color.X / updatesLeft);
color.Y -= (color.Y / updatesLeft);
color.Z -= (color.Z / updatesLeft);
color.W -= (color.W / updatesLeft);
_color = new Color(color);
Color = new Color(color);
}

public override void Update()
Expand All @@ -74,7 +74,7 @@ public override void Update()
else
{
// Target reached, Kill this instance.
((Stage)_parentView).removePlayerFeedback(this);
((Stage)_view).removePlayerFeedback(this);
}
}
}
Expand Down
Loading

1 comment on commit 48462bc

@MaxWinsemius
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to use popups:

Standart margin of 5px and standart border of 3 px. Standart colors following color theme.

List g = new List ();
g.Add (new Text (new Vector2 (0), _game, this, "Aero Matics Display-18", "Current level:", Color.White));
g.Add (new Text (new Vector2 (0, 30), _game, this, "Aero Matics Display-36", Model.Rat.level.ToString (), Color.White));
_gameObjects.Add(new PopupBox(_game, this, g, true));

Please sign in to comment.