aboutsummaryrefslogtreecommitdiff
path: root/src/particles.h
blob: 101fc49ce145cb8cb5a0bd72f8e8315318ec6543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef PARTICLES_HEADER
#define PARTICLES_HEADER

#define DIGGING_PARTICLES_AMOUNT 10

#include <iostream>
#include "irrlichttypes_extrabloated.h"
#include "tile.h"
#include "localplayer.h"
#include "environment.h"

class Particle : public scene::ISceneNode
{
	public:
	Particle(
		IGameDef* gamedef,
		scene::ISceneManager* mgr,
		LocalPlayer *player,
		ClientEnvironment &env,
		v3f pos,
		v3f velocity,
		v3f acceleration,
		float expirationtime,
		float size,
		bool collisiondetection,
		bool vertical,
		video::ITexture *texture,
		v2f texpos,
		v2f texsize
	);
	~Particle();

	virtual const core::aabbox3d<f32>& getBoundingBox() const
	{
		return m_box;
	}

	virtual u32 getMaterialCount() const
	{
		return 1;
	}

	virtual video::SMaterial& getMaterial(u32 i)
	{
		return m_material;
	}

	virtual void OnRegisterSceneNode();
	virtual void render();

	void step(float dtime);

	bool get_expired ()
	{ return m_expiration < m_time; }

private:
	void updateLight();
	void updateVertices();

	video::S3DVertex m_vertices[4];
	float m_time;
	float m_expiration;

	ClientEnvironment *m_env;
	IGameDef *m_gamedef;
	core::aabbox3d<f32> m_box;
	core::aabbox3d<f32> m_collisionbox;
	video::SMaterial m_material;
	v2f m_texpos;
	v2f m_texsize;
	v3f m_pos;
	v3f m_velocity;
	v3f m_acceleration;
	LocalPlayer *m_player;
	float m_size;
	u8 m_light;
	bool m_collisiondetection;
	bool m_vertical;
	v3s16 m_camera_offset;
};

class ParticleSpawner
{
	public:
	ParticleSpawner(IGameDef* gamedef,
		scene::ISceneManager *smgr,
		LocalPlayer *player,
		u16 amount,
		float time,
		v3f minp, v3f maxp,
		v3f minvel, v3f maxvel,
		v3f minacc, v3f maxacc,
		float minexptime, float maxexptime,
		float minsize, float maxsize,
		bool collisiondetection,
		bool vertical,
		video::ITexture *texture,
		u32 id);

	~ParticleSpawner();

	void step(float dtime, ClientEnvironment &env);

	bool get_expired ()
	{ return (m_amount <= 0) && m_spawntime != 0; }

	private:
	float m_time;
	IGameDef *m_gamedef;
	scene::ISceneManager *m_smgr;
	LocalPlayer *m_player;
	u16 m_amount;
	float m_spawntime;
	v3f m_minpos;
	v3f m_maxpos;
	v3f m_minvel;
	v3f m_maxvel;
	v3f m_minacc;
	v3f m_maxacc;
	float m_minexptime;
	float m_maxexptime;
	float m_minsize;
	float m_maxsize;
	video::ITexture *m_texture;
	std::vector<float> m_spawntimes;
	bool m_collisiondetection;
	bool m_vertical;
};

void allparticles_step (float dtime);
void allparticlespawners_step (float dtime, ClientEnvironment &env);

void delete_particlespawner (u32 id);
void clear_particles ();

void addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
	LocalPlayer *player, ClientEnvironment &env, v3s16 pos,
	const TileSpec tiles[]);

void addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
	LocalPlayer *player, ClientEnvironment &env, v3s16 pos,
	const TileSpec tiles[]);

void addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr,
	LocalPlayer *player, ClientEnvironment &env, v3s16 pos,
	const TileSpec tiles[]);

#endif
ight (C) 2010-2013 blue42u, Jonathon Anderson <anderjon@umail.iu.edu> Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net> This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "hud.h" #include "main.h" #include "settings.h" #include "util/numeric.h" #include "log.h" #include "gamedef.h" #include "itemdef.h" #include "inventory.h" #include "tile.h" #include "localplayer.h" #include "camera.h" #include "porting.h" #include <IGUIStaticText.h> #ifdef HAVE_TOUCHSCREENGUI #include "touchscreengui.h" #endif Hud::Hud(video::IVideoDriver *driver, scene::ISceneManager* smgr, gui::IGUIEnvironment* guienv, gui::IGUIFont *font, u32 text_height, IGameDef *gamedef, LocalPlayer *player, Inventory *inventory) { this->driver = driver; this->smgr = smgr; this->guienv = guienv; this->font = font; this->text_height = text_height; this->gamedef = gamedef; this->player = player; this->inventory = inventory; m_screensize = v2u32(0, 0); m_displaycenter = v2s32(0, 0); m_hotbar_imagesize = floor(HOTBAR_IMAGE_SIZE * porting::getDisplayDensity() + 0.5); m_hotbar_imagesize *= g_settings->getFloat("gui_scaling"); m_padding = m_hotbar_imagesize / 12; const video::SColor hbar_color(255, 255, 255, 255); for (unsigned int i=0; i < 4; i++ ){ hbar_colors[i] = hbar_color; } tsrc = gamedef->getTextureSource(); v3f crosshair_color = g_settings->getV3F("crosshair_color"); u32 cross_r = rangelim(myround(crosshair_color.X), 0, 255); u32 cross_g = rangelim(myround(crosshair_color.Y), 0, 255); u32 cross_b = rangelim(myround(crosshair_color.Z), 0, 255); u32 cross_a = rangelim(g_settings->getS32("crosshair_alpha"), 0, 255); crosshair_argb = video::SColor(cross_a, cross_r, cross_g, cross_b); v3f selectionbox_color = g_settings->getV3F("selectionbox_color"); u32 sbox_r = rangelim(myround(selectionbox_color.X), 0, 255); u32 sbox_g = rangelim(myround(selectionbox_color.Y), 0, 255); u32 sbox_b = rangelim(myround(selectionbox_color.Z), 0, 255); selectionbox_argb = video::SColor(255, sbox_r, sbox_g, sbox_b); use_crosshair_image = tsrc->isKnownSourceImage("crosshair.png"); hotbar_image = ""; use_hotbar_image = false; hotbar_selected_image = ""; use_hotbar_selected_image = false; } void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect, bool selected) { if (selected) { /* draw hihlighting around selected item */ if (use_hotbar_selected_image) { core::rect<s32> imgrect2 = rect; imgrect2.UpperLeftCorner.X -= (m_padding*2); imgrect2.UpperLeftCorner.Y -= (m_padding*2); imgrect2.LowerRightCorner.X += (m_padding*2); imgrect2.LowerRightCorner.Y += (m_padding*2); video::ITexture *texture = tsrc->getTexture(hotbar_selected_image); core::dimension2di imgsize(texture->getOriginalSize()); driver->draw2DImage(texture, imgrect2, core::rect<s32>(core::position2d<s32>(0,0), imgsize), NULL, hbar_colors, true); } else { video::SColor c_outside(255,255,0,0); //video::SColor c_outside(255,0,0,0); //video::SColor c_inside(255,192,192,192); s32 x1 = rect.UpperLeftCorner.X; s32 y1 = rect.UpperLeftCorner.Y; s32 x2 = rect.LowerRightCorner.X; s32 y2 = rect.LowerRightCorner.Y; // Black base borders driver->draw2DRectangle(c_outside, core::rect<s32>( v2s32(x1 - m_padding, y1 - m_padding), v2s32(x2 + m_padding, y1) ), NULL); driver->draw2DRectangle(c_outside, core::rect<s32>( v2s32(x1 - m_padding, y2), v2s32(x2 + m_padding, y2 + m_padding) ), NULL); driver->draw2DRectangle(c_outside, core::rect<s32>( v2s32(x1 - m_padding, y1), v2s32(x1, y2) ), NULL); driver->draw2DRectangle(c_outside, core::rect<s32>( v2s32(x2, y1), v2s32(x2 + m_padding, y2) ), NULL); /*// Light inside borders driver->draw2DRectangle(c_inside, core::rect<s32>( v2s32(x1 - padding/2, y1 - padding/2), v2s32(x2 + padding/2, y1) ), NULL); driver->draw2DRectangle(c_inside, core::rect<s32>( v2s32(x1 - padding/2, y2), v2s32(x2 + padding/2, y2 + padding/2) ), NULL); driver->draw2DRectangle(c_inside, core::rect<s32>( v2s32(x1 - padding/2, y1), v2s32(x1, y2) ), NULL); driver->draw2DRectangle(c_inside, core::rect<s32>( v2s32(x2, y1), v2s32(x2 + padding/2, y2) ), NULL); */ } } video::SColor bgcolor2(128, 0, 0, 0); if (!use_hotbar_image) driver->draw2DRectangle(bgcolor2, rect, NULL); drawItemStack(driver, font, item, rect, NULL, gamedef); } //NOTE: selectitem = 0 -> no selected; selectitem 1-based void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset, InventoryList *mainlist, u16 selectitem, u16 direction) { #ifdef HAVE_TOUCHSCREENGUI if ( (g_touchscreengui) && (offset == 0)) g_touchscreengui->resetHud(); #endif s32 height = m_hotbar_imagesize + m_padding * 2; s32 width = (itemcount - offset) * (m_hotbar_imagesize + m_padding * 2); if (direction == HUD_DIR_TOP_BOTTOM || direction == HUD_DIR_BOTTOM_TOP) { width = m_hotbar_imagesize + m_padding * 2; height = (itemcount - offset) * (m_hotbar_imagesize + m_padding * 2); } // Position of upper left corner of bar v2s32 pos = upperleftpos; if (hotbar_image != player->hotbar_image) { hotbar_image = player->hotbar_image; if (hotbar_image != "") use_hotbar_image = tsrc->isKnownSourceImage(hotbar_image); else use_hotbar_image = false; } if (hotbar_selected_image != player->hotbar_selected_image) { hotbar_selected_image = player->hotbar_selected_image; if (hotbar_selected_image != "") use_hotbar_selected_image = tsrc->isKnownSourceImage(hotbar_selected_image); else use_hotbar_selected_image = false; } /* draw customized item background */ if (use_hotbar_image) { core::rect<s32> imgrect2(-m_padding/2, -m_padding/2, width+m_padding/2, height+m_padding/2); core::rect<s32> rect2 = imgrect2 + pos; video::ITexture *texture = tsrc->getTexture(hotbar_image); core::dimension2di imgsize(texture->getOriginalSize()); driver->draw2DImage(texture, rect2, core::rect<s32>(core::position2d<s32>(0,0), imgsize), NULL, hbar_colors, true); } for (s32 i = offset; i < itemcount && (size_t)i < mainlist->getSize(); i++) { v2s32 steppos; s32 fullimglen = m_hotbar_imagesize + m_padding * 2; core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize); switch (direction) { case HUD_DIR_RIGHT_LEFT: steppos = v2s32(-(m_padding + (i - offset) * fullimglen), m_padding); break; case HUD_DIR_TOP_BOTTOM: steppos = v2s32(m_padding, m_padding + (i - offset) * fullimglen); break; case HUD_DIR_BOTTOM_TOP: steppos = v2s32(m_padding, -(m_padding + (i - offset) * fullimglen)); break; default: steppos = v2s32(m_padding + (i - offset) * fullimglen, m_padding); break; } drawItem(mainlist->getItem(i), (imgrect + pos + steppos), (i +1) == selectitem ); #ifdef HAVE_TOUCHSCREENGUI if (g_touchscreengui) g_touchscreengui->registerHudItem(i, (imgrect + pos + steppos)); #endif } } void Hud::drawLuaElements(v3s16 camera_offset) { for (size_t i = 0; i != player->maxHudId(); i++) { HudElement *e = player->getHud(i); if (!e) continue; v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5), floor(e->pos.Y * (float) m_screensize.Y + 0.5)); switch (e->type) { case HUD_ELEM_IMAGE: { video::ITexture *texture = tsrc->getTexture(e->text); if (!texture) continue; const video::SColor color(255, 255, 255, 255); const video::SColor colors[] = {color, color, color, color}; core::dimension2di imgsize(texture->getOriginalSize()); v2s32 dstsize(imgsize.Width * e->scale.X, imgsize.Height * e->scale.Y); if (e->scale.X < 0) dstsize.X = m_screensize.X * (e->scale.X * -0.01); if (e->scale.Y < 0) dstsize.Y = m_screensize.Y * (e->scale.Y * -0.01); v2s32 offset((e->align.X - 1.0) * dstsize.X / 2, (e->align.Y - 1.0) * dstsize.Y / 2); core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y); rect += pos + offset + v2s32(e->offset.X, e->offset.Y); driver->draw2DImage(texture, rect, core::rect<s32>(core::position2d<s32>(0,0), imgsize), NULL, colors, true); break; } case HUD_ELEM_TEXT: { video::SColor color(255, (e->number >> 16) & 0xFF, (e->number >> 8) & 0xFF, (e->number >> 0) & 0xFF); core::rect<s32> size(0, 0, e->scale.X, text_height * e->scale.Y); std::wstring text = narrow_to_wide(e->text); core::dimension2d<u32> textsize = font->getDimension(text.c_str()); v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2), (e->align.Y - 1.0) * (textsize.Height / 2)); v2s32 offs(e->offset.X, e->offset.Y); font->draw(text.c_str(), size + pos + offset + offs, color); break; } case HUD_ELEM_STATBAR: { v2s32 offs(e->offset.X, e->offset.Y); drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number, offs, e->size); break; } case HUD_ELEM_INVENTORY: { InventoryList *inv = inventory->getList(e->text); drawItems(pos, e->number, 0, inv, e->item, e->dir); break; } case HUD_ELEM_WAYPOINT: { v3f p_pos = player->getPosition() / BS; v3f w_pos = e->world_pos * BS; float distance = floor(10 * p_pos.getDistanceFrom(e->world_pos)) / 10; scene::ICameraSceneNode* camera = smgr->getActiveCamera(); w_pos -= intToFloat(camera_offset, BS); core::matrix4 trans = camera->getProjectionMatrix(); trans *= camera->getViewMatrix(); f32 transformed_pos[4] = { w_pos.X, w_pos.Y, w_pos.Z, 1.0f }; trans.multiplyWith1x4Matrix(transformed_pos); if (transformed_pos[3] < 0) break; f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f : core::reciprocal(transformed_pos[3]); pos.X = m_screensize.X * (0.5 * transformed_pos[0] * zDiv + 0.5); pos.Y = m_screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5); video::SColor color(255, (e->number >> 16) & 0xFF, (e->number >> 8) & 0xFF, (e->number >> 0) & 0xFF); core::rect<s32> size(0, 0, 200, 2 * text_height); std::wstring text = narrow_to_wide(e->name); font->draw(text.c_str(), size + pos, color); std::ostringstream os; os<<distance<<e->text; text = narrow_to_wide(os.str()); pos.Y += text_height; font->draw(text.c_str(), size + pos, color); break; } default: infostream << "Hud::drawLuaElements: ignoring drawform " << e->type << " of hud element ID " << i << " due to unrecognized type" << std::endl; } } } void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset, v2s32 size) { const video::SColor color(255, 255, 255, 255); const video::SColor colors[] = {color, color, color, color}; video::ITexture *stat_texture = tsrc->getTexture(texture); if (!stat_texture) return; core::dimension2di srcd(stat_texture->getOriginalSize()); core::dimension2di dstd; if (size == v2s32()) { dstd = srcd; } else { dstd.Height = size.Y * g_settings->getFloat("gui_scaling") * porting::getDisplayDensity(); dstd.Width = size.X * g_settings->getFloat("gui_scaling") * porting::getDisplayDensity(); offset.X *= g_settings->getFloat("gui_scaling") * porting::getDisplayDensity(); offset.Y *= g_settings->getFloat("gui_scaling") * porting::getDisplayDensity(); } v2s32 p = pos; if (corner & HUD_CORNER_LOWER) p -= dstd.Height; p += offset; v2s32 steppos; switch (drawdir) { case HUD_DIR_RIGHT_LEFT: steppos = v2s32(-1, 0); break; case HUD_DIR_TOP_BOTTOM: steppos = v2s32(0, 1); break; case HUD_DIR_BOTTOM_TOP: steppos = v2s32(0, -1); break; default: steppos = v2s32(1, 0); } steppos.X *= dstd.Width; steppos.Y *= dstd.Height; for (s32 i = 0; i < count / 2; i++) { core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height); core::rect<s32> dstrect(0,0, dstd.Width, dstd.Height); dstrect += p; driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true); p += steppos; } if (count % 2 == 1) { core::rect<s32> srcrect(0, 0, srcd.Width / 2, srcd.Height); core::rect<s32> dstrect(0,0, dstd.Width / 2, dstd.Height); dstrect += p; driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true); } } void Hud::drawHotbar(u16 playeritem) { v2s32 centerlowerpos(m_displaycenter.X, m_screensize.Y); InventoryList *mainlist = inventory->getList("main"); if (mainlist == NULL) { //silently ignore this we may not be initialized completely return; } s32 hotbar_itemcount = player->hud_hotbar_itemcount; s32 width = hotbar_itemcount * (m_hotbar_imagesize + m_padding * 2);