Pages

Alchemist - Alternative Ending!

Ubisoft keeps coming up with games that have alternative endings. So why not me?

A very long time ago, I was forced to read the Alchemist [ at gunpoint ]. This probably is the only non sci-fi, non programming book I've ever read in my life. And to be honest - it sucks, big time. What kinda idiot searches the world to find his destiny. Life is about destiny finding you. Google giving you THE search result is destiny - not you typing the url in the browser.

So here's the Alternative Ending:
Dumb Shepard gets tired of his stupid sheep and their constant "Blaaah Blaah"s.
He sells them.
He buys a dog. [even better - stray dog becomes his friend]
He takes it back to his place.
It digs up.
He finds the treasure.
Lives happily ever after. [Fatima who? We all know long distance relationships never work]

 NFS: Wind doesn't carry kisses. Its probably the smell of.. never mind.


XNA Button

This post is about creating a simple event-based button in XNA. I'll dump the code here. Future self is responsible for understanding it in case he/I don't remember it.


public class Button
{
Texture2D _upTexture, _mouseOverTexture, _mouseDownTexture, _disabledTexture;
int _width, _height, _x, _y;
string _name;
bool _enabled;
Rectangle _drawRectangle;
 
//States
ButtonStates _state;
KeyboardState _prevKeyState;
MouseState _prevMouseState;
 
public bool Enabled
{
get
{
return _enabled;
}
 
set
{
_enabled = value;
}
}
 
public string Name
{
get
{
return _name; 
}
set
{
_name = value;
}
}
 
public int Width
{
get
{
return _width;        
}
set
{
_width = value;
UpdateRectangle();
}
}
 
public int Height
{
get
{
return _height;
}
set
{
_height = value;
UpdateRectangle();
}
}
 
public int Left
{
get
{
return _x;
}
set
{
_x = value;
UpdateRectangle();
}
}
 
public int Top
{
get
{
return _y;
}
set
{
_y = value;
UpdateRectangle();
}
}
 
void UpdateRectangle()
{
_drawRectangle.X = _x;
_drawRectangle.Y = _y;
_drawRectangle.Width = _width;
_drawRectangle.Height = _height;
}
 
public Button(Texture2D up, Texture2D mouseOver, Texture2D mouseDown, Texture2D disabled)
{
_upTexture = up;
_mouseOverTexture = mouseOver;
_mouseDownTexture = mouseDown;
_disabledTexture = disabled;
 
_width = _height = 50;
_x = _y = 0;
_enabled = true;
_drawRectangle = new Rectangle();
_state = ButtonStates.Up;
}
 
public void Update()
{
if (_prevKeyState == null || _prevMouseState == null)
{
_prevMouseState = Mouse.GetState();
_prevKeyState = Keyboard.GetState();
return;
}
 
KeyboardState currentKeyState = Keyboard.GetState();
MouseState currentMouseState = Mouse.GetState();
 
if (_enabled)
{
switch (_state)
{
case ButtonStates.Up:
if (_drawRectangle.Contains(currentMouseState.X, currentMouseState.Y))
{
if (currentMouseState.LeftButton == ButtonState.Pressed && _prevMouseState.LeftButton != ButtonState.Pressed)
{
_state = ButtonStates.MouseDown;
}
else
{
_state = ButtonStates.MouseOver;
}
}
break;
 
case ButtonStates.MouseOver:
if (_drawRectangle.Contains(currentMouseState.X, currentMouseState.Y))
{
if (currentMouseState.LeftButton == ButtonState.Pressed && _prevMouseState.LeftButton != ButtonState.Pressed)
{
_state = ButtonStates.MouseDown;
}
}
else
{
_state = ButtonStates.Up;
}
 
break;
 
case ButtonStates.MouseDown:
if (_drawRectangle.Contains(currentMouseState.X, currentMouseState.Y))
{
if (currentMouseState.LeftButton != ButtonState.Pressed && _prevMouseState.LeftButton == ButtonState.Pressed)
{
_state = ButtonStates.Up;
 
//Fire Click event
if (OnClick != null)
{
OnClick(this);
}
}
}
else
{
if (currentMouseState.LeftButton != ButtonState.Pressed && _prevMouseState.LeftButton == ButtonState.Pressed)
{
//Release outside
_state = ButtonStates.Up;                                
}
}
break;
 
default:
break;
}
}           
 
_prevKeyState = currentKeyState;
_prevMouseState = currentMouseState;

}
 
public void Draw(SpriteBatch spriteBatch)
{
if (_enabled)
{
switch (_state)
{
case ButtonStates.Up:
if (_upTexture != null)
{
spriteBatch.Draw(_upTexture, _drawRectangle, Color.White);
}
break;
case ButtonStates.MouseOver:
if (_mouseOverTexture != null)
{
spriteBatch.Draw(_mouseOverTexture, _drawRectangle, Color.White);
}
break;
case ButtonStates.MouseDown:
if (_mouseDownTexture != null)
{
spriteBatch.Draw(_mouseDownTexture, _drawRectangle, Color.White);
}
break;
default:
break;
}
}
else
{
if (_disabledTexture != null)
{
spriteBatch.Draw(_disabledTexture, _drawRectangle, Color.White);                   
}
}            
}
 
//Events
public delegate void Click(Button sender);
public event Click OnClick;
 
public enum ButtonStates
{
Up,
MouseOver,
MouseDown
}
}

Thats it! Use OnClick event in the game to tap the click event, call button.Update() in the update logic of the game code.

Some Screenshots!






Risks

I recently read a poem about Risks. Its by Janet Rand. Sheer brilliance. Check it out -

To laugh is to risk appearing the fool,
To weep is to risk being called sentimental.
To reach out to another is to risk involvement.
To expose feelings is to risk showing your true self.
To place your ideas and your dreams before the crowd is to risk being called naive.
To love is to risk not being loved in return,
To live is to risk dying,
To hope is to risk despair,
To try is to risk failure

But risks must be taken, because the greatest risk in life is to risk nothing.
The person who risks nothing, does nothing, has nothing, is nothing, and becomes nothing.
He may avoid suffering and sorrow, but he simply cannot learn, feel, change, grow or love.
Chained by his certitude, he is a slave; he has forfeited his freedom.
Only the person who risks is truly free.

- JANET RAND

Destiny and the Irish Pot of Gold Syndrome

I always believe that nothing can be done to avoid one's destiny. It happens no matter what. There's no way to avoid events from happening in life - no matter how much you try, just like in final destination.

Theorem 1: When destiny wants to kick your ass - All you can do is bend over.

Conclusion 1: You never know whats in store for you. When you bend over - you may actually find a Pot-of-Gold. It's just destiny's way of showing you something good in life.

Corollary 1: When you find a Pot-of-Gold and bend over to pick it up - boy you MAYBE in trouble!

NFS: Stock markets may be a safer bet than investing in gold!