polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

movement.c (2351B)


      1 
      2 #include "engine.h"
      3 
      4 void movement(struct engine *engine, struct node *node, float speed_mult)
      5 {
      6 	float amt = 3.0 * engine->dt;
      7 	float turn = 1.0 * engine->dt;
      8 	
      9 	float x_axis = (float)engine->input.axis[0] / (float)MAX_AXIS_VALUE;
     10 	float y_axis = (float)engine->input.axis[1] / (float)MAX_AXIS_VALUE;
     11 	
     12 	amt *= speed_mult;
     13 	
     14 	if ((engine->input.modifiers & KMOD_SHIFT) /*||
     15 	    is_button_down(&engine->input, SDL_CONTROLLER_BUTTON_LEFTSTICK)*/) {
     16 		amt *= 20;
     17 	}
     18 	
     19 	// joystick movement
     20 	node_forward(node, V3(0,amt*y_axis, 0));
     21 	node_forward(node, V3(amt*x_axis, 0, 0));
     22 	
     23 	if (engine->input.keystates[SDL_SCANCODE_A])
     24 		node_forward(node, V3(-amt,0,0));
     25 	
     26 	if (engine->input.keystates[SDL_SCANCODE_UP])
     27 		node_forward(node, V3(0,0,amt));
     28 	
     29 	if (engine->input.keystates[SDL_SCANCODE_DOWN])
     30 		node_forward(node, V3(0,0,-amt));
     31 	
     32 	if (engine->input.keystates[SDL_SCANCODE_D])
     33 		node_forward(node, V3(amt,0,0));
     34 	
     35 	if (engine->input.keystates[SDL_SCANCODE_W])
     36 		node_forward(node, V3(0,amt,0));
     37 	
     38 	if (engine->input.keystates[SDL_SCANCODE_S])
     39 		node_forward(node, V3(0,-amt,0));
     40 	
     41 	if (engine->input.keystates[SDL_SCANCODE_K])
     42 		node_forward(node, V3(0, 0,amt));
     43 	
     44 	if (engine->input.keystates[SDL_SCANCODE_J])
     45 		node_forward(node, V3(0, 0,-amt));
     46 	
     47 	if (engine->input.keystates[SDL_SCANCODE_E])
     48 		node_rotate(node, V3(0, 0, turn));
     49 	
     50 	if (engine->input.keystates[SDL_SCANCODE_Q])
     51 		node_rotate(node, V3(0, 0, -turn));
     52 	
     53 	/* if (engine->input.keystates[SDL_SCANCODE_DOWN]) */
     54 	/*   node_translate(node, V3(0, 0, -amt)); */
     55 	
     56 	if (was_key_pressed_this_frame(engine, SDL_SCANCODE_P)) {
     57 	    debug("player %f %f %f quat %f %f %f %f\n",
     58 	            node->pos[0],
     59 	            node->pos[1],
     60 	            node->pos[2],
     61 	            node->orientation[0],
     62 	            node->orientation[1],
     63 	            node->orientation[2],
     64 	            node->orientation[3]
     65 	          );
     66 	}
     67 }
     68 
     69 
     70 void entity_jump(struct entity *ent, float amount)
     71 {
     72     float dir[3];
     73     vec3_normalize(ent->velocity, dir);
     74     vec3_add(dir, V3(0, 0, amount), dir);
     75     vec3_add(ent->velocity, dir, ent->velocity);
     76 }
     77 
     78 
     79 void gravity(struct entity *ent, float dt) {
     80     if (ent->flags & ENT_ON_GROUND)
     81         return;
     82 
     83     struct node *pnode = get_node(&ent->node_id);
     84     assert(pnode);
     85 
     86     static const float g = -1.0;
     87     vec3_add(ent->velocity, V3(0.0, 0.0, g * dt), ent->velocity);
     88 }
     89