-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.cpp
154 lines (132 loc) · 3.51 KB
/
actor.cpp
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
#include "actor.h"
#include "level.h"
#include "fpumath.h"
#include "kvstore.h"
CoreType Actor::s_Type( &Actor::Super::s_Type );
void Actor::PreDeltaUpdate( float dt )
{
unlinkFromMap( m_level->GetMap(), this, m_pos );
m_onGround = false;
m_vel[2] -= 1.f * dt;
if ( m_vel[2] <= 0.f &&
issolid( m_level->GetMap(), (int)(m_pos[0]), (int)(m_pos[1]), (int)(m_pos[2] + m_vel[2] - 0.1f) ) )
{
m_vel[2] = 0.f;
m_onGround = true;
}
if ( m_onGround )
{
m_vel[0] = 0.f;
m_vel[1] = 0.f;
}
#if 0
if ( attach.ent != -1 )
{
entitystate_s &attachee = entities[player->attach.ent];
int index = -1;
for ( unsigned int i=0; i<attachee.mesh.m_attachments.size(); i++ )
{
if ( attachee.mesh.m_attachments[i].name == player->attach.name )
{
index = i;
break;
}
}
if ( index != -1 )
{
AnimMeshAttach &attach = attachee.mesh.m_attachments[index];
calcAttachMtx( player->wmtx, attachee.mesh, attach.idx, attach.ofs, attach.rot, attachee.wmtx );
player->pos[0] = player->wmtx[12];
player->pos[1] = player->wmtx[13];
player->pos[2] = player->wmtx[14];
}
}
#endif
}
void Actor::PostDeltaUpdate( float dt )
{
mtxRotateZ(m_wmtx, m_rotz );
m_wmtx[12] = m_pos[0];
m_wmtx[13] = m_pos[1];
m_wmtx[14] = m_pos[2];
linkToMap( m_level->GetMap(), this, m_pos );
if ( m_health < 0.f )
{
m_level->RemoveEntity( this );
}
}
void Actor::OnAddToLevel( Level *l )
{
Super::OnAddToLevel( l );
l->AddDelta( this );
l->AddRender( this );
}
void Actor::OnRemoveFromLevel( Level *l )
{
l->RemoveDelta( this );
l->RemoveRender( this );
Super::OnRemoveFromLevel( l );
}
void Actor::Spawn( KVStore const &kv )
{
Super::Spawn( kv );
m_rotz = kv.GetKeyValueFloat( "rotz" );
m_health = kv.GetKeyValueFloat( "health" );
}
bool Actor::AnimUpdate( int &tags, const char *animName, float dt )
{
float v = m_vel[0]*m_vel[0] + m_vel[1]*m_vel[1];
m_moveSpeed = v;
float prevTime = m_animTime;
m_animTime += dt;
const AnimMesh &mesh = *m_meshDef->GetMesh();
int animIndex = 0;
for ( unsigned int i=0; i<mesh.m_anims.size(); i++ )
{
if ( mesh.m_anims[i].name == animName )
{
animIndex = i;
break;
}
}
//if ( animIndex != -1 )
{
tags = animTags( mesh.m_anims[animIndex], prevTime, m_animTime );
float *animStack = (float*)alloca( sizeof(float)*16*mesh.m_submeshs.size() );
for ( unsigned int i=0; i<mesh.m_submeshs.size(); i++ )
{
const AnimSubMesh &am = mesh.m_submeshs[i];
AnimSubMeshJoint &aj = m_meshInstance.m_submeshPose[i];
for ( unsigned int j=0; j<3; j++ )
{
float val = animSample( mesh.m_anims[animIndex], i, j, m_animTime, mesh.m_submeshs[i].baseofs[j] );
aj.animofs[j] = val;
}
for ( unsigned int j=0; j<3; j++ )
{
float val = animSample( mesh.m_anims[animIndex], i, 3+j, m_animTime, mesh.m_submeshs[i].baserot[j] );
aj.animrot[j] = val;
}
float *mtx = &animStack[i*16];
mtxRotateXYZ(mtx, aj.animrot[0], aj.animrot[1], aj.animrot[2] );
mtx[12] = aj.animofs[0];
mtx[13] = aj.animofs[1];
mtx[14] = aj.animofs[2];
memcpy( aj.animmtx, mtx, sizeof(float)*16 );
}
for ( unsigned int i=0; i<mesh.m_skeleton.size(); i++ )
{
const AnimSkeleton &as = mesh.m_skeleton[i];
float *c = &animStack[as.child*16];
float *f = m_meshInstance.m_submeshPose[as.child].animmtx;
float *p = m_meshInstance.m_submeshPose[as.parent].animmtx;
mtxMul( f, c, p );
}
}
return !mesh.m_anims[animIndex].looping && m_animTime >= mesh.m_anims[animIndex].time;
}
void Actor::Attack( Entity *by )
{
m_flashTime = m_level->GetTimeMS() + 500;
m_health -= 1.f;
}