// The player is alive
void
alive(Player * me)
{
	InputState * is;
	Camera * cam;
	EntitySet * others;
	Level world;

	unsigned int mousex;
	unsigned int mousey;

	float playerx, playery;

	float fake_lookx, fake_looky;
	float lookx, looky;
	unsigned int dir;

	float x, y;

	float dx = 0;
	float dy = 0;

	gboolean shoot;
	gboolean firing;

	Entity * target;

	is = me->is;
	cam = me->cam;
	others = me->others;
	world = me->world;

	firing = mouse_press(is);
	shoot = expired(me->attack_timer); 

	// Get the mouse position
	mouse_position(is, &mousex, &mousey);

	// Get the mouse position with regards to the level.
	camera_inverse_transform(cam, mousex, mousey, &fake_lookx, &fake_looky);
	lookx = fake_lookx / square_size;
	looky = fake_looky / square_size;

	entity_position(me->body, &playerx, &playery);

	dir = look(playerx, playery, lookx, looky);

	// Set the direction of the player with regards to key presses
	if (key_down(is, SDLK_a) || key_down(is, SDLK_LEFT))
	{
		dx = -1;
	} else if (key_down(is, SDLK_d) || key_down(is, SDLK_RIGHT))
	{
		dx = 1;
	}

	if (key_down(is, SDLK_w) || key_down(is, SDLK_UP))
	{
		dy = -1;
	} else if (key_down(is, SDLK_s) || key_down(is, SDLK_DOWN))
	{
		dy = 1;
	}

	entity_set_direction(me->body, dx, dy);

	// Try to shoot a KITTY!
	if (firing)
	{
		if (shoot)
		{
			play_wav(me->fire);

			entity_position(me->body, &x, &y);

			if (can_see(world, x, y, lookx, looky))
			{
				target = collision(me->body, lookx, looky, others);



				if (target)
				{
					entity_destroy(target);
					me->score ++;
				}
			}
		}


		if (dx != 0 || dy != 0)
		{
			if (dir == LEFT)
			{
				set_animation(me->body, (char *) "shoot_walk_left");
			}
			else if (dir == RIGHT)
			{
				set_animation(me->body, (char *) "shoot_walk_right");
			}
			else if (dir == UP)
			{
				set_animation(me->body, (char *) "shoot_walk_up");
			}
			else if (dir == DOWN)
			{
				set_animation(me->body, (char *) "shoot_walk_down");
			}
		}
		else
		{
			if (dir == LEFT)
			{
				set_animation(me->body, (char *) "shoot_idle_left");
			}
			else if (dir == RIGHT)
			{
				set_animation(me->body, (char *) "shoot_idle_right");
			}
			else if (dir == UP)
			{
				set_animation(me->body, (char *) "shoot_idle_up");
			}
			else if (dir == DOWN)
			{
				set_animation(me->body, (char *) "shoot_idle_down");
			}

		}
	}
	else
	{
		if (dx != 0 || dy != 0)
		{
			if (dir == LEFT)
			{
				set_animation(me->body, (char *) "walk_left");
			}
			else if (dir == RIGHT)
			{
				set_animation(me->body, (char *) "walk_right");
			}
			else if (dir == UP)
			{
				set_animation(me->body, (char *) "walk_up");
			}
			else if (dir == DOWN)
			{
				set_animation(me->body, (char *) "walk_down");
			}
		}
		else
		{
			if (dir == LEFT)
			{
				set_animation(me->body, (char *) "idle_left");
			}
			else if (dir == RIGHT)
			{
				set_animation(me->body, (char *) "idle_right");
			}
			else if (dir == UP)
			{
				set_animation(me->body, (char *) "idle_up");
			}
			else if (dir == DOWN)
			{
				set_animation(me->body, (char *) "idle_down");
			}

		}

	}

}
	event_node_ptr fake_event_source::mouse_click(const size_t time, const Uint8 button)
	{
		mouse_press(time, button);
		return mouse_release(time+1,button);
	}
Example #3
0
window::window( const std::shared_ptr<platform::window> &win )
	: _window( win )
{
	precondition( bool(_window), "null window" );
	_window->exposed = [this] ( void ) { paint(); };
	_window->resized = [this] ( double w, double h ) { resized( w, h ); };
	_window->mouse_pressed = [this]( const std::shared_ptr<platform::mouse> &, const base::point &p, int b ) { mouse_press( p, b ); };
	_window->mouse_released = [this]( const std::shared_ptr<platform::mouse> &, const base::point &p, int b ) { mouse_release( p, b ); };
	_window->mouse_moved = [this]( const std::shared_ptr<platform::mouse> &, const base::point &p ) { mouse_moved( p ); };
	_window->mouse_wheel = [this]( const std::shared_ptr<platform::mouse> &, int i ) { mouse_wheel( i ); };
	_window->key_pressed = [this]( const std::shared_ptr<platform::keyboard> &, const platform::scancode &c ) { key_pressed( c ); };
	_window->key_released = [this]( const std::shared_ptr<platform::keyboard> &, const platform::scancode &c ) { key_released( c ); };
	_window->text_entered = [this]( const std::shared_ptr<platform::keyboard> &, const char32_t &c ) { text_entered( c ); };
	_canvas = std::make_shared<draw::canvas>();
}