Improved block picking

This commit is contained in:
Joshua Barretto 2020-07-06 23:04:13 +01:00
parent 62b59de95a
commit 30619771af
3 changed files with 25 additions and 24 deletions

View File

@ -50,7 +50,7 @@ void main() {
// Select glowing
if (select_pos.w > 0 && select_pos.xyz == floor(sprite_pos)) {
f_col = f_col * 8.0;
f_col *= 8.0;
}
f_light = 1.0;

View File

@ -21,6 +21,10 @@ out vec4 tgt_color;
#include <sky.glsl>
#include <light.glsl>
float vmax(vec3 v) {
return max(v.x, max(v.y, v.z));
}
void main() {
// First 3 normals are negative, next 3 are positive
vec3 normals[6] = vec3[](vec3(-1,0,0), vec3(1,0,0), vec3(0,-1,0), vec3(0,1,0), vec3(0,0,-1), vec3(0,0,1));
@ -46,6 +50,14 @@ void main() {
diffuse_light += point_light * ao;
vec3 col = f_col + hash(vec4(floor(f_chunk_pos * 3.0 - f_norm * 0.5), 0)) * 0.02; // Small-scale noise
// Select glowing
if (select_pos.w > 0 && select_pos.xyz == floor(f_pos - f_norm * 0.01)) {
if (vmax(abs(mod(f_pos - f_norm * 0.5, 1.0) - 0.5)) > 0.45) {
col *= 0.5;
}
}
vec3 surf_color = illuminate(srgb_to_linear(col), light, diffuse_light, ambient_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);

View File

@ -256,6 +256,13 @@ impl PlayState for SessionState {
}
};
let can_build = self.client
.borrow()
.state()
.read_storage::<comp::CanBuild>()
.get(self.client.borrow().entity())
.is_some();
// Only highlight collectables
self.scene.set_select_pos(select_pos.filter(|sp| {
self.client
@ -263,7 +270,7 @@ impl PlayState for SessionState {
.state()
.terrain()
.get(*sp)
.map(|b| b.is_collectible())
.map(|b| b.is_collectible() || can_build)
.unwrap_or(false)
}));
@ -279,16 +286,9 @@ impl PlayState for SessionState {
return PlayStateResult::Shutdown;
},
Event::InputUpdate(GameInput::Primary, state) => {
// Check the existence of CanBuild component. If it's here, use LMB to
// break blocks, if not, use it to attack
// If we can build, use LMB to break blocks, if not, use it to attack
let mut client = self.client.borrow_mut();
if state
&& client
.state()
.read_storage::<comp::CanBuild>()
.get(client.entity())
.is_some()
{
if state && can_build {
if let Some(select_pos) = select_pos {
client.remove_block(select_pos);
}
@ -302,13 +302,7 @@ impl PlayState for SessionState {
let mut client = self.client.borrow_mut();
if state
&& client
.state()
.read_storage::<comp::CanBuild>()
.get(client.entity())
.is_some()
{
if state && can_build {
if let Some(build_pos) = build_pos {
client.place_block(build_pos, self.selected_block);
}
@ -319,12 +313,7 @@ impl PlayState for SessionState {
Event::InputUpdate(GameInput::Roll, state) => {
let client = self.client.borrow();
if client
.state()
.read_storage::<comp::CanBuild>()
.get(client.entity())
.is_some()
{
if can_build {
if state {
if let Some(block) = select_pos
.and_then(|sp| client.state().terrain().get(sp).ok().copied())